Problem:
Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

public class Main {
	int N;
	int[] vals;
	String S;
	int result = 0;
	Map<String, String> cache;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		this.N = Integer.parseInt(in.readLine());
		this.vals = new int[N];
		S = in.readLine();
		for (int i = 0; i < N; ++i) {
			vals[i] = Integer.parseInt(S.substring(i, i + 1));
		}
		cache = new HashMap<>();
	}

	int calc(int[] array) {
		if (array.length == 1) {
			return array[0];
		} else if (array.length == 2) {
			return Math.abs(array[1] - array[0]);
		} else {
			int[] next = new int[array.length - 1];
			for (int i = 0; i + 1 < array.length; ++i) {
				next[i] = Math.abs(array[i + 1] - array[i]);
			}
			return calc(next);
		}
	}

	void initCache(int index, int[] array) {
		if (index == 0) {
			// cacheに値を入れる
			StringBuilder strBuilder = new StringBuilder();
			for (int i = 0; i < array.length; ++i) {
				strBuilder.append(array[i]);
			}
			int val = calc(array);
			cache.put(strBuilder.toString(), Integer.toString(val));
		} else {
			for (int i = 0; i < 4; ++i) {
				array[index - 1] = i;
				initCache(index - 1, array);
			}
		}
	}

	int calc(String str) {
		int[] array = new int[str.length()];
		for (int i = 0; i < str.length(); ++i) {
			array[i] = Integer.parseInt(str.substring(i, i + 1));
		}
		return calc(array);
	}

	int subCalc(String str) {
		if (str.length() <= 8) {
			return calc(str);
		} else {
			StringBuilder next = new StringBuilder();
			for (int i = 0; i + 7 < str.length(); ++i) {
				String s = cache.get(str.substring(i, i + 8));
				next.append(s);
			}
			return subCalc(next.toString());
		}
	}

	void calc() {
		int[] array = new int[8];
		initCache(8, array);
		result = subCalc(S);
	}

	void showResult() {
		System.out.println(result);
	}

}