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 C# (Mono-csc 3.5.0) (judge verdict: Accepted)

using System;

namespace AtCoder {
    class Unbalanced {
        static bool can(string S, int H) {
            int from = 0, to = H;
            bool parityFlag = false;
            for (int i = 0; i < S.Length; i++) {
                if (from == to) {
                    parityFlag = true;
                }

                if (!parityFlag) {
                    if (S[i] == '0') {
                        if (from == H) {
                            return false;
                        }

                        from++;
                        to = Math.Min(to + 1, H);
                    } else if (S[i] == '1') {
                        if (to == 0) {
                            return false;
                        }

                        from = Math.Max(from - 1, 0);
                        to--;
                    } else {
                        from = Math.Max(from - 1, 0);
                        to = Math.Min(to + 1, H);
                    }
                } else {
                    if (S[i] == '0') {
                        if (from == H) {
                            return false;
                        }

                        from++;
                        to = (to == H ? to - 1 : to + 1);
                    } else if (S[i] == '1') {
                        if (to == 0) {
                            return false;
                        }
                        from = (from == 0 ? 1 : from - 1);
                        to--;
                    } else {
                        from = (from == 0 ? 1 : from - 1);
                        to = (to == H ? to - 1 : to + 1);
                        if (from > to) {
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        static void Main(string[] args) {
            string S = Console.ReadLine();
            int N = S.Length;

            int L = 0, R = N;

            while (R - L > 1) {
                int H = (L + R) / 2;
                if (can(S, H)) {
                    R = H;
                } else {
                    L = H;
                }
            }

            Console.WriteLine(R);
        }
    }
}