QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#6272. Klein bottle

Statistics

Given an $(n+1)\times (n+1)$ grid graph, where each point has coordinates $(i, j)$ with $0\leq i, j\leq n$. For each $(i, j)$:

  • For $j\geq 1$, there is an undirected edge to $(i, j-1)$, and there is an undirected edge between $(i, 0)$ and $(i, n)$.
  • For $i\geq 1$, there is an undirected edge to $(i-1, j)$, and there is an undirected edge between $(0, j)$ and $(n, n-j)$. Note that $j$ is connected to $n-j$ here, so you can think of this as a Klein bottle.

Given two points on the Klein bottle for each query, find the length of the shortest path between them.

Input

The first line contains two positive integers $n$ and $q$.

Each of the next $q$ lines contains four integers $x_1, y_1, x_2, y_2$, representing the coordinates of the two points $(x_1, y_1)$ and $(x_2, y_2)$.

Output

Output $q$ lines, each containing the answer to the corresponding query in order.

Examples

Input 1

10 5
1 9 10 1
7 4 5 4
1 1 3 1
6 6 0 2
10 2 6 1

Output 1

2
2
2
7
5

Subtasks

For $100\%$ of the data, $1\leq n\leq 10^8$, $1\leq q\leq 10^5$, and $0\leq x_1, y_1, x_2, y_2\leq n$.

For test cases $1\sim 3$, $n, q\leq 10$.

For test cases $4\sim 5$, $n\leq 10$.

For test cases $6\sim 7$, $n\leq 10^3$.

For test cases $8\sim 9$, $q=1$.

For test case $10$, there are no additional constraints.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.