#include <bits/stdc++.h>
using namespace std;
int f(string& S, vector<int>& s, vector<pair<int, int> >& v, int mn) {
  int pos = 0;
  int cnt = 0;
  int mx = 0;
  int i;
  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;
    mx = max(mx, t);
  }
  return mx;
}
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 mn[] = {v[0].first - 1, v[0].first};
  int res = min(f(S, s, v, mn[0]) - mn[0], f(S, s, v, mn[1]) - mn[1]);
  cout << res << endl;
}
int main() {
  int T, t;
  T = 1;
  for (t = (0); t < (T); ++t) SolveTest(t);
  return 0;
};
