#include <bits/stdc++.h>
using namespace std;
string s;
pair<int, int> inc(pair<int, int> u, pair<int, int> goodRange) {
  if (u.first == u.second && u.second == goodRange.second) return {1e9, -1e9};
  u.first++;
  u.second++;
  if (u.second > goodRange.second) u.second -= 2;
  return u;
}
pair<int, int> dec(pair<int, int> u, pair<int, int> goodRange) {
  if (u.first == u.second && u.first == goodRange.first) return {1e9, -1e9};
  u.first--;
  u.second--;
  if (u.first < goodRange.first) u.first += 2;
  return u;
}
bool check(int delta) {
  pair<int, int> goodRange(-delta, delta);
  pair<int, int> curPos(0, 0);
  for (int it = 0; it < 6; it++) {
    for (char u : s) {
      if (u == '1') {
        curPos = inc(curPos, goodRange);
      } else if (u == '0') {
        curPos = dec(curPos, goodRange);
      } else {
        pair<int, int> curPos1 = inc(curPos, goodRange);
        pair<int, int> curPos2 = dec(curPos, goodRange);
        curPos.first = min(curPos1.first, curPos2.first);
        curPos.second = max(curPos1.second, curPos2.second);
      }
      if (curPos.first > curPos.second) return 0;
      goodRange.first = max(goodRange.first, curPos.first - delta);
      goodRange.second = min(goodRange.second, curPos.second + delta);
    }
    reverse(s.begin(), s.end());
    for (char &u : s) {
      if (u == '1')
        u = '0';
      else if (u == '0')
        u = '1';
    }
  }
  return 1;
}
void solve(int test) {
  cin >> s;
  int low = 0, high = 1000010;
  while (high - low > 1) {
    int mid = low + high >> 1;
    if (check(mid))
      high = mid;
    else
      low = mid;
  }
  cout << high << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  tc = 1;
  for (int test = 1; test <= tc; test++) {
    solve(test);
  }
  return 0;
}
