Fancy Formulas

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

You are given a prime pp and a pair of integers (a,b)(a, b) such that their sum is not divisible by pp. In one operation, you can do one of the following:

  • Replace (a,b)(a, b) with (2amodp,(b+pa)modp)(2a \bmod p, (b+p-a) \bmod p)
  • Replace (a,b)(a, b) with ((a+pb)modp,2bmodp)((a+p-b) \bmod p, 2b \bmod p)

You have to answer qq queries. In the ii-th query, find the smallest number of operations needed to transform the pair (a_i,b_i)(a\_i, b\_i) into the pair (c_i,d_i)(c\_i, d\_i), or determine that it is impossible.

Note that the order of numbers matters. For example, for p=3p = 3, the distance between (1,2)(1, 2) and (2,1)(2, 1) is 11, not 00.

입력

The first line contains two integers pp and qq (2p109+72 \le p \le 10^9 + 7, pp is prime, 1q1051 \le q \le 10^5): the prime and the number of queries to answer.

The ii-th of the next qq lines contains four integers a_ia\_i, b_ib\_i, c_ic\_i, d_id\_i (0a_i,b_i,c_i,d_i<p0 \le a\_i, b\_i, c\_i, d\_i < p, and a_i+b_ia\_i+b\_i is not divisible by pp).

출력

For each query, if it is impossible to transform (a_i,b_i)(a\_i, b\_i) into (c_i,d_i)(c\_i, d\_i), output 1-1. Otherwise, output the smallest number of operations required to achieve this goal.