#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  string S;
  cin >> S;
  int N = int(S.size());
  int low = 0, high = N;
  while (low < high) {
    int mid = (low + high) >> 1;
    bool good = false;
    for (int z = 0; z < 2; ++z) {
      int l = z, r = mid;
      if ((r - l) & 1) --r;
      bool fail = false;
      for (int i = 0; i < N; ++i) {
        if (S[i] == '1') {
          ++l, ++r;
        } else if (S[i] == '0') {
          --l, --r;
        } else {
          --l, ++r;
        }
        if (l < 0) l += 2;
        if (r > mid) r -= 2;
        if (l > r) {
          fail = true;
          break;
        }
      }
      if (!fail) good = true;
    }
    if (good)
      high = mid;
    else
      low = mid + 1;
  }
  cout << low << "\n";
  return 0;
}
