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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#![allow(bare_trait_objects)]
#![allow(unused_imports)]
#![allow(dead_code)]

use std::fmt::Debug;
use std::io::BufRead;
use std::io::Stdin;
use std::io::StdinLock;
use std::str::FromStr;

pub struct Scanner<'a> {
    buf_read: Box<BufRead + 'a>,
    tokens: Vec<String>,
}

impl<'a> Scanner<'a> {
    pub fn new(buf_read: StdinLock<'a>) -> Self {
        Scanner {
            buf_read: Box::new(buf_read),
            tokens: Vec::new(),
        }
    }

    // pub fn from_stdin() -> Self {
    //     let stdin = Box::leak(Box::new(std::io::stdin()));
    //     Self {
    //         buf_read: Box::new(stdin.lock()),
    //         tokens: Vec::new(),
    //     }
    // }

    pub fn next(&mut self) -> String {
        while self.tokens.is_empty() {
            let mut s = String::new();
            if self.buf_read.read_line(&mut s).is_err() {
                panic!()
            }
            self.tokens = s
                .split_whitespace()
                .filter(|s| !s.is_empty())
                .rev()
                .map(str::to_string)
                .collect();
        }
        self.tokens.pop().unwrap()
    }

    pub fn next_g<T: FromStr>(&mut self) -> T
    where
        T::Err: Debug,
    {
        self.next().parse().unwrap()
    }

    pub fn next_vec_g<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where
        T::Err: Debug,
    {
        (0..n).map(|_| self.next_g()).collect()
    }

    pub fn usize(&mut self) -> usize {
        self.next_g()
    }

    pub fn i64(&mut self) -> i64 {
        self.next_g()
    }

    pub fn vec_i64(&mut self, n: usize) -> Vec<i64> {
        self.next_vec_g(n)
    }

    pub fn vec_usize(&mut self, n: usize) -> Vec<usize> {
        self.next_vec_g(n)
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.next().chars().collect()
    }
}

fn main() {
    let stdin = std::io::stdin();
    let mut sc = Scanner::new(stdin.lock());

    let n = sc.usize();
    let mut x = Vec::new();

    {
        let cs: Vec<_> = sc.chars().iter().map(|c| *c as i32 - '0' as i32).collect();
        let mut cur = (-1, 0);
        for i in 0..cs.len() - 1 {
            let v = (cs[i] - cs[i + 1]).abs();
            if cur.0 != v {
                if cur.0 >= 0 {
                    x.push(cur);
                }
                cur.0 = v;
                cur.1 = 0;
            }
            cur.1 += 1;
        }
        x.push(cur);
    }

    let mut next = Vec::new();
    for _ in 0..n - 2 {
        // eprintln!("{:?}", x);
        let mut cur = (-1, 0);
        {
            let mut add = |c: (i32, usize)| {
                if cur.0 != c.0 {
                    if cur.0 >= 0 {
                        next.push(cur);
                    }
                    cur.0 = c.0;
                    cur.1 = 0;
                }
                cur.1 += c.1;
            };

            for i in 0..x.len() {
                if x[i].1 > 1 {
                    add((0, x[i].1 - 1));
                }
                if i + 1 < x.len() {
                    add(((x[i].0 - x[i + 1].0).abs(), 1));
                }
            }
        }
        next.push(cur);
        std::mem::swap(&mut x, &mut next);
        next.clear();
    }

    println!("{}", x[0].0);
}