#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1& a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1& a, T2 b) {
  if (a < b) a = b;
}
std::random_device rd;
std::mt19937 mt(rd());
constexpr long long MOD = 1e9 + 7;
constexpr int MAX = 500050;
const long double pi = acos(-1);
constexpr long double EPS = 1e-10;
constexpr long long LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
string S;
string T;
int dpr[2][1000010], dpl[2][1000010];
int N;
bool check(int x) {
  S = T;
  for (int j = 0; j < (2); ++j) {
    for (int i = 0; i < (N + 2); ++i) {
      dpr[j][i] = 0;
      dpl[j][i] = 0;
    }
  }
  for (int i = N - 1; i >= 0; --i) {
    int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
    dpr[1][i] = max(0, res1);
    int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
    dpr[0][i] = max(0, res0);
    if (res0 > x || res1 > x) {
      return false;
    }
  }
  for (int i = 0; i < (N); ++i) {
    if (S[i] == '?') {
      int res0 = dpl[0][i] + dpr[0][i + 1];
      int res1 = dpl[1][i] + dpr[1][i + 1];
      if (res0 < res1)
        S[i] = '0';
      else
        S[i] = '1';
      res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
      res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
      dpl[0][i + 1] = max(0, res0);
      dpl[1][i + 1] = max(0, res1);
      if (res0 > x || res1 > x) return false;
    }
  }
  return true;
}
void solve() {
  cin >> S;
  T = S;
  N = S.size();
  int ok = N, ng = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << (ok) << '\n';
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
