#include <bits/stdc++.h>
using namespace std;
void SolveTest(int test) {
  string S;
  cin >> S;
  int i, j;
  int sum = 0;
  vector<pair<int, int> > v;
  vector<int> s(int((S).size()));
  for (i = (0); i < (int((S).size())); ++i) {
    sum += S[i] == '0' ? -1 : 1;
    s[i] = sum;
    v.push_back(pair<int, int>(sum, i));
  }
  sort((v).begin(), (v).end());
  int pos = 0;
  int cnt = 0;
  int mn = 0, mx = 0;
  for (i = (0); i < (int((S).size())); ++i) {
    while (pos != int((v).size()) && v[pos].second < i) ++pos;
    if (S[i] == '?' && pos != int((v).size())) {
      int val = v[pos].first;
      int ind = v[pos].second;
      if (val - 2 * cnt > mn + 1) ++cnt;
    }
    int t = s[i] - 2 * cnt;
    mn = min(mn, t);
    mx = max(mx, t);
  }
  cout << (mx - mn) << endl;
}
int main() {
  int T, t;
  T = 1;
  for (t = (0); t < (T); ++t) SolveTest(t);
  return 0;
};
