Problem:
Bozosort

　

Bozosort, as well as Bogosort, is a very inefficient sort algorithm. It is a random number-based algorithm that sorts sequence elements following the steps as below:

Randomly select two elements and swap them.

Verify if all the elements are sorted in increasing order.

Finish if sorted, else return to 1.

To analyze Bozosort, you have decided to simulate the process using several predetermined pairs of elements.

You are given several commands to swap two elements. Make a program to evaluate how many times you have to run the command before the sequence is aligned in increasing order.

Input

The input is given in the following format.

$N$
$a_1$ $a_2$ ... $a_N$
$Q$
$x_1$ $y_1$
$x_2$ $y_2$
$...$
$x_Q$ $y_Q$

The first line provides the number of sequence elements $N$ ($2 \leq N \leq 300,000$). The second line provides an array of integers $a_i$ ($1 \leq a_i \leq 10^9$) that constitutes the sequence. Each of the subsequent $Q$ lines provides a pair of integers $x_i,y_i$ ($1 \leq x_i,y_i \leq N$) that represent the $i$-th command, which swaps the two elements indicated by $x_i$ and $y_i$ ($x_i \ne y_i$).

Output

If neat alignment in increasing order is realized the first time after execution of multiple commands, output at what time it was. Output 0 if the initial sequence is aligned in increasing order, and -1 if exhaustive execution still failed to attain the goal.

Sample Input 1

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

Sample Output 1

2

Sample Input 2

4
4 3 2 1
2
1 2
3 4

Sample Output 2

-1

Sample Input 3

5
1 1 1 2 2
1
1 2

Sample Input 3

0

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _0385
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = RInt();
            int[] a = RArInt();
            int q = RInt();
            int[][] xy = new int[q][];
            for (int i = 0; i < q; i++) { xy[i] = RArInt(); }

            int[] correct = a.OrderBy(x => x).ToArray();
            int wrongCnt = 0;
            for (int i = 0; i < a.Length; i++) if (a[i] != correct[i]) wrongCnt++;

            if (wrongCnt == 0) Console.WriteLine(0);
            else
            {
                int res = -1;
                for (int i = 0; i < q; i++)
                {
                    int x = xy[i][0] - 1, y = xy[i][1] - 1;
                    if (a[x] == correct[x] && a[y] != correct[x]) wrongCnt++;
                    if (a[y] == correct[y] && a[x] != correct[y]) wrongCnt++;
                    if (a[x] != correct[x] && a[y] == correct[x]) wrongCnt--;
                    if (a[y] != correct[y] && a[x] == correct[y]) wrongCnt--;
                    if (wrongCnt == 0)
                    {
                        res = i + 1;
                        break;
                    }
                    int tmp = a[x]; a[x] = a[y]; a[y] = tmp;
                }
                Console.WriteLine(res);
            }
        }

        static string RSt() { return Console.ReadLine(); }
        static int RInt() { return int.Parse(Console.ReadLine().Trim()); }
        static long RLong() { return long.Parse(Console.ReadLine().Trim()); }
        static double RDouble() { return double.Parse(Console.ReadLine()); }
        static string[] RArSt(char sep = ' ') { return Console.ReadLine().Trim().Split(sep); }
        static int[] RArInt(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Trim().Split(sep), e => int.Parse(e)); }
        static long[] RArLong(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Trim().Split(sep), e => long.Parse(e)); }
        static double[] RArDouble(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Trim().Split(sep), e => double.Parse(e)); }
        static string WAr<T>(IEnumerable<T> array, string sep = " ") { return string.Join(sep, array.Select(x => x.ToString()).ToArray()); }
    }
}