#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int n = (int)s.size();
  vector<int> a;
  a.push_back(0);
  int p = 0;
  int q = 0;
  int vl = 0, vr = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      p++;
      q++;
    } else if (s[i] == '1') {
      p--;
      q--;
    } else {
      p--;
      q++;
    }
    vl = max(vl, p);
    vr = max(vr, q);
  }
  const int inf = 1e9 + 7;
  auto f = [&](int x, string s) {
    int q = 0;
    vector<int> st;
    bool bad = false;
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        q++;
      } else if (s[i] == '1') {
        q--;
      } else {
        st.push_back(i + 1);
        s[i] = '1';
        q--;
      }
      while (q < x && !st.empty()) {
        int i = st.back();
        st.pop_back();
        s[i - 1] = '0';
        q += 2;
      }
      if (q < x) {
        bad = true;
      }
    }
    q = 0;
    vector<int> a = {0};
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        q++;
      } else {
        q--;
      }
      a.push_back(q);
    }
    return *max_element(a.begin(), a.end()) - *min_element(a.begin(), a.end());
  };
  int ans = 1e9;
  auto ok = [&](int y) {
    int me = f(y, s);
    for (int lg = (1 << 20); lg > 0; lg /= 2) {
      int go = f(y + lg, s);
      if (go < me) {
        me = go;
        y += lg;
      }
      go = f(y - lg, s);
      if (go < me) {
        me = go;
        y -= lg;
      }
    }
    for (int i = y - 5; i <= y + 5; i++) {
      ans = min(ans, f(i, s));
    }
  };
  ok(vl);
  ok(vr);
  cout << ans << endl;
}
