#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
char s[N];
int n, now, mn[N], mx[N], m, sum, smx[N], smn[N], Smx, Smn, ans = 1e9;
void done() {
  int Smx = mx[1], Smn = mn[1], ans = 0;
  sum = 0;
  for (int i = 1; i < m; i++) {
    int d1 = max(Smx - mn[i + 1] - sum - 2, mx[i + 1] - Smn + sum + 2),
        d2 = max(Smx - mn[i + 1] - sum, mx[i + 1] - Smn + sum);
    if (d1 < d2)
      sum += 2;
    else if (d1 == d2)
      if (rand() & 1) sum += 2;
    int dd1 = mn[i + 1] + sum, dd2 = mx[i + 1] + sum;
    ans = max(ans, max(Smx - dd1, dd2 - Smn));
    Smx = max(dd2, Smx);
    Smn = min(dd1, Smn);
  }
  ::ans = min(::ans, ans);
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1, j; i <= n; i++) {
    if (s[i] == '?') {
      m++;
      mn[m] = mx[m] = now;
      continue;
    }
    j = i;
    while (j < n && s[j + 1] != '?') j++;
    m++;
    mn[m] = now;
    mx[m] = now;
    for (int k = i; k <= j; k++) {
      if (s[k] == '0')
        now--;
      else
        now++;
      mn[m] = min(mn[m], now);
      mx[m] = max(mx[m], now);
    }
    i = j + 1;
  }
  if (s[n] == '?') m++, mn[m] = mx[m] = now;
  for (int i = 2; i <= m; i++) mn[i] -= (i - 1), mx[i] -= (i - 1);
  for (int i = 1; i <= m; i++) ans = max(ans, mx[i] - mn[i]);
  smx[m] = mx[m];
  smn[m] = mn[m];
  for (int i = m - 1; i; i--)
    smn[i] = min(smn[i + 1], mn[i]), smx[i] = max(smx[i + 1], mx[i]);
  Smx = -1e9;
  Smn = 1e9;
  while (1.0 * clock() / CLOCKS_PER_SEC < 1.8) done();
  cout << ans;
  return 0;
}
