using System;
using CompLib.Collections.Generic;
using CompLib.Util;

public class Program
{
    private string S;
    private int N;

    public void Solve()
    {
        var sc = new Scanner();
        S = sc.Next();
        N = S.Length;

        /*
         * 1を +1 0を-1とする
         * 累積和　最大 - 最小
         *
         * 最大がMのとき最小をf(M)とする
         * f(M) ?全部 -1にする
         * +1に変えてもMを超えないなら+にする貪欲
         *
         * Mを2増やす
         * 貪欲で+に変わるのは高々1個
         *
         * f(M+2) <= F(M) + 2
         *
         * よって
         * 
         * (M+2) - F(M+2) >= M + 2 - F(M) - 2
         *                 = M - F(M)
         * Mは小さい方が良い
         */

        int minM = 0;
        int tmp = 0;
        for (int i = 0; i < N; i++)
        {
            tmp += S[i] == '1' ? 1 : -1;
            minM = Math.Max(minM, tmp);
        }

        Console.WriteLine(Math.Min(minM - F(minM), (minM + 1) - F(minM + 1)));
    }

    int F(int m)
    {
        var st = new SegmentTree<int>(N + 1, Math.Max, int.MinValue);

        int tmp = 0;
        for (int i = 0; i < N; i++)
        {
            st[i] = tmp;
            tmp += S[i] == '1' ? 1 : -1;
        }

        st[N] = tmp;

        int min = 0;
        int plus = 0;
        for (int i = 0; i < N; i++)
        {
            if (S[i] == '?')
            {
                // +にする
                // 以降の累積和+2
                if (st.Query(i + 1, N + 1) + plus + 2 <= m)
                {
                    plus += 2;
                }
            }

            min = Math.Min(min, st[i + 1] + plus);
        }

        return min;
    }


    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Collections.Generic
{
    using System;

    public class SegmentTree<T>
    {
        private readonly int N;
        private T[] _array;

        private T _identity;
        private Func<T, T, T> _operation;

        public SegmentTree(int size, Func<T, T, T> operation, T identity)
        {
            N = 1;
            while (N < size) N *= 2;
            _identity = identity;
            _operation = operation;
            _array = new T[N * 2];
            for (int i = 1; i < N * 2; i++)
            {
                _array[i] = _identity;
            }
        }

        /// <summary>
        /// A[i]をnに更新 O(log N)
        /// </summary>
        /// <param name="i"></param>
        /// <param name="n"></param>
        public void Update(int i, T n)
        {
            i += N;
            _array[i] = n;
            while (i > 1)
            {
                i /= 2;
                _array[i] = _operation(_array[i * 2], _array[i * 2 + 1]);
            }
        }

        private T Query(int left, int right, int k, int l, int r)
        {
            if (r <= left || right <= l)
            {
                return _identity;
            }

            if (left <= l && r <= right)
            {
                return _array[k];
            }

            return _operation(Query(left, right, k * 2, l, (l + r) / 2),
                Query(left, right, k * 2 + 1, (l + r) / 2, r));
        }

        /// <summary>
        /// A[left] op A[left+1] ... op A[right-1]を求める
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public T Query(int left, int right)
        {
            return Query(left, right, 1, 0, N);
        }

        public T this[int i]
        {
            set { Update(i, value); }
            get { return _array[i + N]; }
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}

Measured memory_bytes: 38432