#ds403. 带权并查集

带权并查集

Weighted Union-Find

Problem Description

You are given nn variables a1,a2,…,ana_1, a_2, \dots, a_n whose values are initially unknown.

You must perform qq operations:

  • 1 l r x: gives a clue that al−ar=xa_l - a_r = x. If this contradicts the known information, ignore this operation.
  • 2 l r: answer al−ara_l - a_r. If the current clues do not allow deducing the answer, ignore this operation.

Input Format

The first line contains two integers nn and qq (1≤n≤2×1051 \le n \le 2 \times 10^5, 1≤q≤4×1051 \le q \le 4 \times 10^5).

The following qq lines each describe one operation. The input is encrypted as follows:

There is a variable tt initially 00. After every query operation that is not ignored, tt becomes the absolute value of the answer, i.e. ∣al−ar∣|a_l - a_r|. Each operation's input gives encrypted l′,r′l', r'; the actual values are l=(l′+t) mod n+1l = (l' + t) \bmod n + 1 and r=(r′+t) mod n+1r = (r' + t) \bmod n + 1.

It is guaranteed that 1≤l′,r′≤n1 \le l', r' \le n and −109≤x≤109-10^9 \le x \le 10^9.

Output Format

For each query operation that is not ignored, output its answer. If a query operation is ignored, output nothing for it.

Sample Input

3 5
1 1 2 1
2 1 3
1 2 3 1
2 1 3
2 1 3

Sample Output

2
-1