#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long INF = 1LL << 60;
const long long mod = 1e9 + 7;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int n;
string s;
bool check(int x) {
  int ma = 0, mi = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      if (ma - 1 < -x) return false;
      ma--;
      if (mi - 1 < -x) {
        mi++;
      }
    } else if (s[i] == '1') {
      if (mi + 1 > x) return false;
      mi++;
      if (ma + 1 > x) {
        ma--;
      }
    } else {
      if (ma + 1 > x)
        --ma;
      else
        ++ma;
      if (mi - 1 < -x)
        ++mi;
      else
        --mi;
    }
  }
  return true;
}
void solve() {
  cin >> s;
  n = (int)s.size();
  int ok = n, ng = -1;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
