#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int fdiv2(int x);
bool check(string& s, int n, int d1, int d2);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.length();
  int lo = 1, hi = n - 1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int d1 = mid / 2, d2 = (mid + 1) / 2;
    if (check(s, n, d1, d2) or check(s, n, d2, d1)) {
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  int ans = hi + 1;
  int delta = 0, cnt = 0, bmax = 0, bmin = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '?') {
      ++cnt;
    } else {
      delta += (s[i] == '0' ? 1 : -1);
    }
    bmax = max(bmax, delta - cnt);
    bmin = min(bmin, delta + cnt);
  }
  lo = 0;
  hi = n;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (check(s, n, bmax, mid)) {
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  ans = min(ans, bmax + hi + 1);
  lo = 0;
  hi = n;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (check(s, n, mid, -bmin)) {
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  ans = min(ans, -bmin + hi + 1);
  cout << ans << endl;
}
int fdiv2(int x) { return (x > 0 ? x / 2 : -((-x + 1) / 2)); }
bool check(string& s, int n, int d1, int d2) {
  vector<vector<int>> mx(2, vector<int>(n, n));
  int cnt = 0, delta = 0;
  bool ok = true;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '?') {
      ++cnt;
    } else {
      delta += (s[i] == '0' ? 1 : -1);
    }
    mx[0][i] = fdiv2(cnt - delta + d1);
    mx[1][i] = fdiv2(cnt + delta + d2);
  }
  for (int i = n - 2; i >= 0; --i) {
    mx[0][i] = min(mx[0][i], mx[0][i + 1]);
    mx[1][i] = min(mx[1][i], mx[1][i + 1]);
  }
  ok &= (mx[0][0] >= 0 and mx[1][0] >= 0);
  int c0 = 0, c1 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] != '?') {
      continue;
    } else if (c0 >= mx[0][i] and c1 >= mx[1][i]) {
      ok = false;
      break;
    } else if (c0 < mx[0][i]) {
      ++c0;
    } else {
      ++c1;
    }
  }
  return ok;
}
