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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {
  private static InputReader in;
  private static PrintWriter out;

  public static void main(String[] args) throws IOException {
    in = new InputReader(System.in);
    out = new PrintWriter(System.out, true);

    int n = in.nextInt();
    int[][] arr = new int[3][n];
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++) {
        arr[i][j] = in.nextInt();
      }
    }
    boolean ok = true;
    int[] nums = new int[n];
    long b1 = 0, b2 = 0;
    for (int col = 0; col < n; col++) {
      int num = -1;
      if (arr[0][col] < arr[1][col]) {
        if (arr[1][col] != arr[0][col]+1 || arr[2][col] != arr[1][col]+1) {
          ok = false;
          break;
        }
        if ((arr[0][col]-1) % 3 != 0) {
          ok = false;
          break;
        }
        num = (arr[0][col]-1)/3;
      } else {
        if (arr[1][col] != arr[0][col]-1 || arr[2][col] != arr[1][col]-1) {
          ok = false;
          break;
        }
        if ((arr[2][col]-1) % 3 != 0) {
          ok = false;
          break;
        }
        num = (arr[2][col]-1)/3;
        if (col % 2 == 0) b2++;
        else b1++;
      }
      nums[col] = num;
      if (num%2 != col%2) {
        ok = false;
        break;
      }
      if (num % 2 == 0) b2 += 2*n+num/2-col/2;
      else b1 += 2*n+num/2-col/2;
      if (num < col) {
        if (col % 2 == 0) b1 += (col/2-num/2);
        else b2 += (col/2-num/2);
      }
    }
    out.println(ok && b1%2 == 0 && b2%2 == 0 ? "Yes" : "No");
    
    out.close();
    System.exit(0);
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream), 32768);
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }
  }


}