#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int isPos(int u) {
  int l = 0, h = u;
  bool d = false;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      l--, h--;
    else if (s[i] == '1')
      l++, h++;
    else
      l--, h++;
    if (!d)
      l = max(l, 0), h = min(h, u);
    else {
      if (l < 0) l += 2;
      if (h > u) h -= 2;
    }
    if (l == h) d = true;
    if (l > h) return false;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> s;
  n = ((int)((s).size()));
  int l = -1, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (isPos(m))
      r = m;
    else
      l = m;
  }
  cout << r << "\n";
}
