Problem:
Score : 800 points

Problem Statement

Given is a string S, where each character is 0, 1, or ?.

Consider making a string S' by replacing each occurrence of ? with 0 or 1 (we can choose the character for each ? independently).
Let us define the unbalancedness of S' as follows:

(The unbalancedness of S') = \max \{ The absolute difference between the number of occurrences of 0 and 1 between the l-th and r-th character of S (inclusive) :\ 1 \leq l \leq r \leq |S|\}

Find the minimum possible unbalancedness of S'.

Constraints

1 \leq |S| \leq 10^6

Each character of S is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible unbalancedness of S'.

Sample Input 1

0??

Sample Output 1

1

We can make S'= 010 and achieve the minimum possible unbalancedness of 1.

Sample Input 2

0??0

Sample Output 2

2

Sample Input 3

??00????0??0????0?0??00??1???11?1?1???1?11?111???1

Sample Output 3

4

Solution in Rust (1.42.0) (judge verdict: Accepted)

use proconio::*;
use proconio::marker::Chars;

fn valid(s: &[char], p: i32, parity: i32) -> bool {
    let mut l = parity;
    let mut r = if p % 2 == 0 {
        p / 2 * 2 - parity
    } else {
        p / 2 * 2 + parity
    };
//    println!("query {} {}, s: {} {}", p, parity, l, r);
    for &c in s.iter() {
        match c {
            '0' => {
                l += 1;
                r += if r + 1 > p {
                    -1
                } else {
                    1
                };
            },
            '1' => {
                l -= if l == 0 {
                    -1
                } else {
                    1
                };
                r -= 1;
            },
            _ => {
                l -= if l == 0 {
                    -1
                } else {
                    1
                };
                r += if r + 1 > p {
                    -1
                } else {
                    1
                };
            },
        }
        if l > r {
            return false;
        }
    }
    true
}

fn run() {
    input! {
        s: Chars,
    }
    let mut ng = 0;
    let mut ok = 1_000_000;
    while ok - ng > 1 {
        let mid = (ok + ng) / 2;
        if valid(&s, mid, 0) || valid(&s, mid, 1) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ok);
}

fn main() {
    run();
}