#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n;
char s[N];
bool chk(int mid) {
  int k1 = 0, k2 = mid;
  for (int i = (1); i <= (n); ++i) {
    if (s[i] == '0')
      --k1, --k2;
    else if (s[i] == '1')
      ++k1, ++k2;
    else
      --k1, ++k2;
    if (k1 < 0) k1 += 2;
    if (k2 > mid) k2 -= 2;
    if (k1 > k2) return 0;
  }
  return 1;
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  int l = 0, r = 1e6, ans = 1e6;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (chk(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
