Problem:
Score : 1500 points

Problem Statement

We have a grid with 3 rows and N columns.
The cell at the i-th row and j-th column is denoted (i, j).
Initially, each cell (i, j) contains the integer i+3j-3.

A grid with N=5 columns

Snuke can perform the following operation any number of times:

Choose a 3×3 subrectangle of the grid. The placement of integers within the subrectangle is now rotated by 180°.

An example sequence of operations (each chosen subrectangle is colored blue)

Snuke's objective is to manipulate the grid so that each cell (i, j) contains the integer a_{i,j}.
Determine whether it is achievable.

Constraints

5≤N≤10^5

1≤a_{i,j}≤3N

All a_{i,j} are distinct.

Input

The input is given from Standard Input in the following format:

N
a_{1,1} a_{1,2} ... a_{1,N}
a_{2,1} a_{2,2} ... a_{2,N}
a_{3,1} a_{3,2} ... a_{3,N}

Output

If Snuke's objective is achievable, print Yes. Otherwise, print No.

Sample Input 1

5
9 6 15 12 1
8 5 14 11 2
7 4 13 10 3

Sample Output 1

Yes

This case corresponds to the figure in the problem statement.

Sample Input 2

5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Sample Output 2

No

Sample Input 3

5
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15

Sample Output 3

Yes

The objective is already achieved with the initial placement of the integers.

Sample Input 4

6
15 10 3 4 9 16
14 11 2 5 8 17
13 12 1 6 7 18

Sample Output 4

Yes

Sample Input 5

7
21 12 1 16 13 6 7
20 11 2 17 14 5 8
19 10 3 18 15 4 9

Sample Output 5

No

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = int.Parse(Console.ReadLine());
        int[] A = new int[N];
        bool[] R = new bool[N];
        {
            string[] str0 = Console.ReadLine().Split(' ');
            string[] str1 = Console.ReadLine().Split(' ');
            string[] str2 = Console.ReadLine().Split(' ');
            for(int i=0;i<N;i++){
                int x = int.Parse(str0[i]);
                int y = int.Parse(str1[i]);
                int z = int.Parse(str2[i]);
                if(x+1 == y && y+1 == z && z%3 == 0){
                    A[i] = y/3;
                    R[i] = (i%4 >= 2) ^ (A[i]%4 >= 2) ^ true;
                }
                else if(x-1 == y && y-1 == z && x%3 == 0){
                    A[i] = y/3;
                    R[i] = (i%4 >= 2) ^ (A[i]%4 >= 2);
                }
                else{
                    sb.Append("No\n");
                    return;
                }
                if((A[i] - i) % 2 != 0){
                    sb.Append("No\n");
                    return;
                }
            }
        }
        {
            int[] P = new int[(N+1)/2];
            int[] E = new int[(N+1)/2];
            bool b = true;
            for(int i=0;i<(N+1)/2;i++){
                P[A[i*2]/2] = i;
                E[i] = A[i*2]/2;
            }
            for(int i=0;i<(N+1)/2;i++){
                if(P[i] != i){
                    b = !b;
                    int temp = E[i];
                    P[E[i]] = P[i];
                    E[i] = E[P[i]];
                    E[P[i]] = temp;
                    P[i] = i;
                }
            }
            for(int i=0;i<N/2;i++){
                if(!R[i*2+1]){
                    b = !b;
                }
            }
            if(!b){
                sb.Append("No\n");
                return;
            }
        }
        {
            int[] P = new int[N/2];
            int[] E = new int[N/2];
            bool b = true;
            for(int i=0;i<N/2;i++){
                P[A[i*2+1]/2] = i;
                E[i] = A[i*2+1]/2;
            }
            for(int i=0;i<N/2;i++){
                if(P[i] != i){
                    b = !b;
                    int temp = E[i];
                    P[E[i]] = P[i];
                    E[i] = E[P[i]];
                    E[P[i]] = temp;
                    P[i] = i;
                }
            }
            for(int i=0;i<(N+1)/2;i++){
                if(!R[i*2]){
                    b = !b;
                }
            }
            if(!b){
                sb.Append("No\n");
                return;
            }
        }
        sb.Append("Yes\n");
    }
}