#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int MX = 1e9 + 7;
const long long int INF = 1LL * MX * MX;
int n;
char in[N];
int limit[N];
int check(int v) {
  int zeroes = 0, ones = 0, mx = 0;
  for (int i = 1; i <= n; ++i) {
    char c = in[i];
    if (c == '0') {
      ++zeroes;
    } else if (c == '1') {
      ++ones;
      mx = max(mx, ones - zeroes);
    } else {
      if (zeroes - ones + limit[i] + 1 <= v)
        ++zeroes;
      else {
        ++ones;
        mx = max(mx, ones - zeroes);
      }
    }
  }
  return mx + v;
}
int main() {
  scanf("%s", in + 1);
  n = strlen(in + 1);
  int mx = 0;
  for (int i = n; i >= 1; --i) {
    limit[i] = mx;
    if (in[i] == '0') {
      ++mx;
    } else {
      --mx;
      if (mx < 0) mx = 0;
    }
  }
  int s = mx, e = n;
  while (s < e) {
    int mid = (s + e) / 2;
    if (check(mid) < check(mid + 1))
      e = mid;
    else
      s = mid + 1;
  }
  printf("%d\n", check(s));
  return 0;
}
