#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i != v.size() - 1) cout << endl;
  };
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  cout << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <typename T>
void Out(T x) {
  cout << x << endl;
}
template <typename T1, typename T2>
void Ans(bool f, T1 y, T2 n) {
  if (f)
    Out(y);
  else
    Out(n);
}
using vec = vector<long long>;
using mat = vector<vec>;
using Pii = pair<long long, long long>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;
const long long mod = 1000000007;
signed main() {
  string S;
  cin >> S;
  long long N = ((long long)S.size());
  long long n0 = 0, n1 = N;
  while (n1 - n0 > 1) {
    long long n = (n0 + n1) / 2;
    bool ok = true;
    for (long long q = (0); q < (2); q++) {
      long long c = 0;
      long long m = 0, M = 0, now = 0;
      for (long long i = (0); i < (N); i++) {
        if (S[i] == '?')
          c++;
        else {
          if (c > 0) {
            if (c > n) {
              if ((c - n) % 2 == 0)
                c = n;
              else
                c = n - 1;
            }
            now -= c;
            c = 0;
            M = max(M, now - m);
            m = min(m, now);
          }
          if (S[i] == '0' + q)
            now++;
          else
            now--;
          M = max(M, now - m);
          m = min(m, now);
        }
      }
      if (c > 0) {
        if (c > n) {
          if ((c - n) % 2 == 0)
            c = n;
          else
            c = n - 1;
        }
        now -= c;
        c = 0;
        M = max(M, now - m);
        m = min(m, now);
      }
      if (M > n) ok = false;
    }
    if (ok)
      n1 = n;
    else
      n0 = n;
  }
  Out(n1);
  return 0;
}
