#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
long long rnd() {
  unsigned long long ans = 0;
  for (int i = (int)(0); i <= (int)(4); i++) ans = ans << 15 ^ rand();
  return ans % ((unsigned long long)1 << 63);
}
const int N = 1000005;
char ch[N];
int ans;
int main() {
  scanf("%s", ch + 1);
  int n = strlen(ch + 1);
  int x = 0, y = 0;
  for (int i = (int)(n); i >= (int)(1); i--) {
    if (ch[i] == '0') {
      x++;
      y--;
    } else if (ch[i] == '1') {
      x--;
      y++;
    } else {
      x--;
      y--;
    }
    x = max(x, 0);
    y = max(y, 0);
    ans = max(ans, max(x, y));
  }
  ans = 1e9;
  for (int o = (int)(0); o <= (int)(1); o++) {
    int mx = 0, l = o, r = o, sum = 1;
    if (ch[1] == '1' && o) continue;
    if (ch[1] == '0' && !o) continue;
    for (int i = (int)(2); i <= (int)(n); i++) {
      if (ch[i] == '0') {
        l++;
        r++;
        if (l > sum) {
          l = r = ++sum;
          mx = 0;
        } else if (r > sum) {
          r = sum - 1;
          mx |= 2;
        } else if (r == sum) {
          mx &= 1;
        }
      } else if (ch[i] == '1') {
        l--;
        r--;
        if (r < 0) {
          sum++;
          l = r = 0;
          mx = 0;
        } else if (l < 0) {
          l = 1;
          mx |= 1;
        } else if (l == 0) {
          mx &= 2;
        }
      } else {
        l--;
        r++;
        if (r > sum) {
          r = sum - 1;
          mx |= 2;
        } else if (r == sum) {
          mx &= 1;
        }
        if (l < 0) {
          l = 1;
          mx |= 1;
        } else if (l == 0) {
          mx &= 2;
        }
      }
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
}
