#include <bits/stdc++.h>
using ull = unsigned long long;
using namespace std;
namespace mydef {
const long long INF = 1ll << 60;
const long long MOD = 1e9 + 7;
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return 0;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return 0;
}
void Yes(bool flag = true) {
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
bool dbg = false;
}  // namespace mydef
using namespace mydef;
string S;
bool check(long long d) {
  long long low = 0;
  long long high = d;
  for (auto &c : S) {
    if (c == '?') {
      if (high == 0) {
        low = 1;
        high = 1;
      } else if (low == d) {
        low = d - 1;
        high = d - 1;
      } else {
        low = max(low - 1, 0ll);
        high = min(high + 1, d);
      }
    } else if (c == '0') {
      if (high == 0) return false;
      low = max(low - 1, 0ll);
      high--;
    } else if (c == '1') {
      if (low == d) return false;
      low++;
      high = min(high + 1, d);
    }
    if (low > high) {
      return false;
    }
  }
  return true;
}
void solve() {
  check(1);
  long long ok = 1000001;
  long long ng = 0;
  while (abs(ok - ng) > 1) {
    long long mid = (ok + ng) / 2;
    if (check(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
}
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> S;
  solve();
  return 0;
}
