#jag2017summerday3k. [jag2017summer_day3_k]Permutation Period
[jag2017summer_day3_k]Permutation Period
MathJax.Hub.Config({ tex2jax: { inlineMath: [[""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 16px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }
Problem Statement
You have a permutation of integers. Initially holds for . For each , let's denote and for any . The period of is defined as the minimum positive integer which satisfies for every .
You are given queries. The -th query is characterized by two distinct indices and . For each query, swap and and then calculate the period of updated modulo in the given order.
It can be proved that the period of always exists.
Input
The input consists of a single test case of the following format.
The first line consists of two integers and (). The -th line consists of two integers and ().
Output
Print the answer in one line for each query.
Sample Input 1
5 4
2 5
2 4
1 3
1 2
Output for Sample Input 1
2
3
6
5
changes as follows: $\[1,2,3,4,5\] \\to \[1,5,3,4,2\] \\to \[1,4,3,5,2\] \\to \[3,4,1,5,2\] \\to \[4,3,1,5,2\]$.
Sample Input 2
2 2
1 2
1 2
Output for Sample Input 2
2
1
changes as follows: \[1,2\] \\to \[2,1\] \\to \[1,2\].
Sample Input 3
10 10
5 6
5 9
8 2
1 6
8 1
7 1
2 6
8 1
7 4
8 10
Output for Sample Input 3
2
3
6
4
6
7
12
7
8
9