#include <bits/stdc++.h>
using namespace std;
const int N = 1000005, M = 1000000007;
int n, i, j, ans, l, r, al, ar;
char c[N];
void rt(int m) {
  l = 0, r = m;
  for (i = strlen(c + 1); i >= 1; --i) {
    if (c[i] == '0') {
      --l;
      --r;
      if (l < 0) l = 0;
    }
    if (c[i] == '1') {
      ++l, ++r;
      if (r > m) r = m;
    }
    if (c[i] == '?') {
      if (l == r) {
        --l;
        ++r;
        if (l < 0) l += 2;
        if (r > m) r -= 2;
      } else {
        --l;
        ++r;
        if (l < 0) ++l;
        if (r > m) --r;
      }
    }
    if (l > r || l > m || r < 0) {
      al = 1;
      ar = 0;
      return;
    }
  }
  al = l, ar = r;
}
bool check(int m) {
  int i;
  rt(m);
  int bl = al, br = ar;
  for (i = 1; c[i]; ++i)
    if (c[i] == '0')
      c[i] = '1';
    else if (c[i] == '1')
      c[i] = '0';
  rt(m);
  for (i = 1; c[i]; ++i)
    if (c[i] == '0')
      c[i] = '1';
    else if (c[i] == '1')
      c[i] = '0';
  bl = m - bl;
  br = m - br;
  swap(bl, br);
  if (min(ar, br) >= max(al, bl))
    return true;
  else
    return false;
}
int main() {
  scanf("%s", c + 1);
  int l = 1, r = strlen(c + 1);
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
}
