#include <bits/stdc++.h>

using namespace std;

#define sz(x) ((int) ((x).size()))
typedef long long ll;
typedef long double ld;

int n;
string s;

int isPos(int u, int sv) {
  int l = sv, h = sv, il = 0, ih = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      l--, h--;
    else if (s[i] == '1')
      l++, h++;
    else
      l--, h++;
    if (l < 0)
      l += 2, il++;
    if (h > u)
      h -= 2, ih++;
    if (l > h)
      return il >= ih ? 1 : 2;
  }
  return 0;
}

bool isPos(int u) {
  int l = -1, r = u;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    int a = isPos(u, m);
    if (a == 0)
      return true;
    if (a == 1)
      l = m;
    else
      r = m;
  }
  return isPos(u, r) == 0;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> s;
  n = sz(s);
  int l = -1, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (isPos(m))
      r = m;
    else
      l = m;
  }
  cout << r << "\n";
}
