#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  long long min_maximum = 0, cur = 0;
  vector<long long> min_offset(1, 0);
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '1') {
      cur += 1;
    } else {
      cur -= 1;
    }
    min_offset.emplace_back(cur);
    min_maximum = max(cur, min_maximum);
  }
  vector<long long> back_max(s.size() + 1, min_offset.back());
  for (int i = s.size() - 1; i >= 0; --i) {
    back_max[i] = max(back_max[i + 1], min_offset[i]);
  }
  long long ans = s.size();
  for (int ub = min_maximum; ub < min_maximum + 2; ++ub) {
    long long cur = 0, maximum = 0, minimum = 0;
    for (int i = 0; i < s.size(); ++i) {
      if (s[i] == '0') {
        cur -= 1;
      } else if (s[i] == '1') {
        cur += 1;
      } else {
        if (cur + 1 + max(0LL, back_max[i + 1] - min_offset[i + 1]) > 0) {
          cur -= 1;
        } else {
          cur += 1;
        }
      }
      maximum = max(maximum, cur);
      minimum = min(minimum, cur);
    }
    ans = min(ans, maximum - minimum);
  }
  cout << ans << endl;
  return 0;
}
