#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
int n, pref[N];
string s;
bool solve(int diff) {
  int j = 0;
  int pos = 0, neg = 0;
  deque<int> mono;
  for (int i = 1; i <= n; ++i) {
    if (s[i - 1] == '0' || s[i - 1] == '?') {
      pref[i] = pref[i - 1] - 1;
    } else {
      pref[i] = pref[i - 1] + 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    while (mono.size() && mono.front() < i) {
      mono.pop_front();
    }
    if (s[i - 1] == '0') {
      pos = max(0, pos - 1);
      neg = neg + 1;
    } else if (s[i - 1] == '1') {
      pos = pos + 1;
      neg = max(0, neg - 1);
    } else {
      j = max(i - 1, j);
      while (j <= n && pref[i - 1] - pref[j] + neg <= diff) {
        j++;
        if (j <= n) {
          while (mono.size() && pref[mono.back()] < pref[j]) {
            mono.pop_back();
          }
          mono.push_back(j);
        }
      }
      assert(mono.size());
      int mx = pref[mono.front()] - pref[i - 1];
      if (mx + pos + 2 > diff || j == n + 1) {
        pos = max(0, pos - 1);
        neg = neg + 1;
      } else {
        pos = pos + 1;
        neg = max(0, neg - 1);
      }
    }
    if (pos > diff || neg > diff) return false;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  n = s.size();
  int lo = 1, hi = n, mid, res;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (solve(mid)) {
      hi = mid - 1;
      res = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << res;
}
