#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
const int mod = 1000000007;
const int inf = 1000000009;
const long long INF = 1000000000000000009;
const long long big = 1000000000000000;
const long double eps = 0.000000000000000000001;
using namespace std;
int M[1000005];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  s = "#" + s;
  int suma = 0, maks = 0, mini;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      suma++;
    else
      suma--;
    maks = max(maks, suma);
  }
  suma = 0;
  for (int i = n; i >= 1; i--) {
    if (s[i] == '1')
      suma++;
    else
      suma--;
    suma = max(suma, 0);
    M[i] = suma;
  }
  int m = maks;
  int wynik = inf;
  for (int j = 1; j <= 2; j++) {
    suma = 0, maks = -inf, mini = inf;
    for (int i = 1; i <= n; i++) {
      if (s[i] == '1')
        suma++;
      else if (s[i] == '0')
        suma--;
      else {
        if (suma + M[i + 1] + 1 <= m)
          suma++;
        else
          suma--;
      }
      maks = max(maks, suma);
      mini = min(mini, suma);
    }
    wynik = min(wynik, maks - mini);
    m++;
  }
  cout << wynik;
  return 0;
}
