#include <bits/stdc++.h>
using namespace std;
string s;
long long n, ans = (long long)1 << 60;
long long dp[1100000];
long long solve(long long ind) {
  long long ma = 0, mi = 0, now = 0;
  for (long long i = 0; i < n; i++) {
    if (s[i] == '0') now--;
    if (s[i] == '1') now++;
    if (s[i] == '?') {
      if (now - 1 - dp[i] >= ind) {
        now--;
      } else {
        now++;
      }
    }
    ma = max(now, ma);
    mi = min(now, mi);
  }
  ans = min(ans, abs(ma - mi));
  return abs(ma - mi);
}
pair<long long, long long> sabu1(int from, int to) {
  for (; to - from > 100;) {
    int mid = (from + to) / 2;
    (solve(mid * 2) - solve((mid - 1) * 2) <= 0 ? from : to) = mid;
  }
  return pair<long long, long long>(from, to);
}
pair<long long, long long> sabu2(int from, int to) {
  for (; to - from > 100;) {
    int mid = (from + to) / 2;
    (solve(mid * 2 - 1) - solve((mid - 1) * 2 - 1) <= 0 ? from : to) = mid;
  }
  return pair<long long, long long>(from, to);
}
int main() {
  cin >> s;
  n = s.size();
  long long now = 0, mi = 0;
  for (long long i = n - 1; i >= 0; i--) {
    dp[i] = now - mi;
    if (s[i] == '0')
      now++;
    else
      now--;
    mi = min(now, mi);
  }
  pair<long long, long long> res = sabu1(-1e6 - 10, 1);
  for (long long i = res.first; i < res.second; i++) {
    solve(i * 2);
  }
  res = sabu2(-1e6 - 10, 1);
  for (long long i = res.first; i < res.second; i++) {
    solve(i * 2 - 1);
  }
  cout << ans << endl;
}
