#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1 << 20, mod = 1e9 + 7;
string s;
int n, ans = 1 << 30;
int p[maxn], sm[maxn], tt[maxn];
void solvep() {
  p[0] = 0;
  int gm = 0;
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + (s[i] == '1' ? -1 : 1);
    gm = min(gm, p[i]);
  }
  sm[n] = p[n];
  for (int i = n - 1; i >= 0; i--) sm[i] = min(p[i], sm[i + 1]);
  int r = 0;
  for (int i = 0; i <= n; i++) {
    sm[i] += r;
    p[i] += r;
    if (s[i] == '?' && sm[i] - 2 >= gm) {
      r -= 2;
      sm[i] -= 2;
      p[i] -= 2;
      s[i] = '1';
    }
    tt[i] = r;
    if (s[i] == '?') s[i] = '0';
  }
  int x = 0, y = 0;
  r = 0;
  for (auto i : s) {
    if (i == '1')
      r--;
    else if (i != '.')
      r++;
    x = min(x, r);
    y = max(y, r);
  }
  ans = min(ans, y - x);
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> s;
  n = s.size();
  s = "." + s;
  for (int i = 0; i < 2; i++) {
    reverse(s.begin(), s.end());
    s.pop_back();
    s = "." + s;
    memset(p, 0, sizeof p);
    memset(sm, 0, sizeof sm);
    solvep();
    memset(p, 0, sizeof p);
    memset(sm, 0, sizeof sm);
    for (auto &i : s)
      if (i != '?' && i != '.') i ^= '0' ^ '1';
    solvep();
  }
  cout << ans;
}
