#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n;
int l[MAXN];
int r[MAXN];
char s[MAXN];
bool ok(int lim) {
  int MIN = lim, MAX = -lim;
  for (int i = 1; i <= n; i++) {
    l[i] = (MAX + i + 1) / 2;
    r[i] = (MIN + i) / 2;
    if (s[i] != '?') {
      l[i] = max(l[i], l[i - 1] + s[i] - '0');
      r[i] = min(r[i], r[i - 1] + s[i] - '0');
    } else {
      l[i] = max(l[i], l[i - 1]);
      r[i] = min(r[i], r[i - 1] + 1);
    }
    MIN = min(MIN, 2 * r[i] - i + lim);
    MAX = max(MAX, 2 * l[i] - i - lim);
  }
  MIN = 1e9, MAX = -1e9;
  for (int i = n; i >= 1; i--) {
    if (i < n) {
      l[i] = max(l[i], (MAX + i + 1) / 2);
      r[i] = min(r[i], (MIN + i) / 2);
      if (s[i + 1] != '?') {
        l[i] = max(l[i], l[i + 1] - (s[i + 1] - '0'));
        r[i] = min(r[i], r[i + 1] - (s[i + 1] - '0'));
      } else {
        l[i] = max(l[i], l[i + 1] - 1);
        r[i] = min(r[i], r[i + 1]);
      }
    }
    MIN = min(MIN, 2 * r[i] - i + lim);
    MAX = max(MAX, 2 * l[i] - i - lim);
  }
  MIN = lim, MAX = -lim;
  for (int i = 1; i <= n; i++) {
    l[i] = max(l[i], (MAX + i + 1) / 2);
    r[i] = min(r[i], (MIN + i) / 2);
    if (s[i] != '?') {
      l[i] = max(l[i], l[i - 1] + s[i] - '0');
      r[i] = min(r[i], r[i - 1] + s[i] - '0');
    } else {
      l[i] = max(l[i], l[i - 1]);
      r[i] = min(r[i], r[i - 1] + 1);
    }
    MIN = min(MIN, 2 * r[i] - i + lim);
    MAX = max(MAX, 2 * l[i] - i - lim);
  }
  for (int i = 1; i <= n; i++)
    if (l[i] > r[i]) return 0;
  return 1;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int low = 1, high = n;
  while (low < high) {
    int mid = low + high >> 1;
    if (ok(mid))
      high = mid;
    else
      low = mid + 1;
  }
  printf("%d\n", low);
  return 0;
}
