#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <istream>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <array>
#include <complex>

#define endl '\n'

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    string s;
    cin >> s;

    vector<int> pre(s.length());

    int x = 0, m = 0;

    for (int i = 0; i < s.length(); ++i)
    {
        x += s[i] == '0' ? +1 : -1;
        m = max(x, m);
        pre[i] = x;
    }

    vector<int> pre_rev = pre;

    for (int i = (int)s.length() - 2; i >= 0; --i)
        pre_rev[i] = max(pre_rev[i], pre_rev[i + 1]);
    pre_rev.push_back(-1000000000);

    int res = s.length();

    for (int j = 0; j < 2; ++j)
    {
        int z = m + j, l = 0, x = 0, k = 0;

        for (int i = 0; i < s.length(); ++i)
        {
            if (s[i] == '?')
            {
                if (max(x + 1, pre_rev[i + 1] + 2 * (k + 1)) <= z)
                {
                    x += 1;
                    k += 1;
                }
                else
                {
                    x--;
                }
            }
            else if (s[i] == '0')
            {
                x++;
            }
            else
            {
                x--;
            }

            l = min(l, x);
        }

        res = min(res, z - l);
    }

    cout << res << endl;

    return 0;
}
