#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int INF = 0x7FFFFFFF;
template <class T>
inline bool checkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool checkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
bool test(string &s, int mx) {
  int a = 0, b = 0;
  int n = int(s.size());
  for (int i = 0; i < (int)n; i++) {
    ;
    ;
    ;
    if (s[i] == '0') {
      a--;
      b--;
      if (b < -mx) b += 2;
      if (b > a) return false;
    } else if (s[i] == '1') {
      a++;
      b++;
      if (a > mx) a -= 2;
      if (b > a) return false;
    } else {
      a++;
      b--;
      if (a > mx) a -= 2;
      if (b < -mx) b += 2;
      if (b > a) return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  while (cin >> s) {
    int n = int(s.size());
    int minX = 0, maxX = 0;
    int sum = 0;
    vector<int> prefix(n + 1);
    for (int i = 0; i < (int)n; i++) {
      if (s[i] == '0')
        sum--;
      else
        sum++;
      prefix[i + 1] = sum;
      checkmin(minX, sum);
      checkmax(maxX, sum);
    };
    ;
    auto fun = [&]() {
      int maxL = 0;
      for (int i = 0; i <= n; i++)
        if (prefix[i] == minX) maxL = i;
      vector<int> right_max(n + 1), right_min(n + 1);
      right_max[n] = right_min[n] = prefix[n];
      for (int i = n - 1; i >= 0; i--) {
        right_max[i] = max(right_max[i + 1], prefix[i]);
        right_min[i] = min(right_min[i + 1], prefix[i]);
      };
      ;
      int del = 0;
      for (int i = maxL + 1; i <= n; i++) {
        if (s[i - 1] == '?' && del + right_min[i] - 2 >= minX) {
          del -= 2;
        };
        prefix[i] += del;
      };
      maxX = 0;
      for (int i = 0; i <= n; i++) checkmax(maxX, prefix[i]);
      return maxX - minX;
    };
    cout << fun() << endl;
  }
  return 0;
}
