#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
}
using Graph = vector<vector<int>>;
using P = pair<int, int>;
using Tuple = tuple<int, int, int>;
int main() {
  string s;
  cin >> s;
  int z = 0, o = 0, q = 0;
  for (long long i = 0; i < (long long)(s.size()); ++i) {
    if (s[i] == '0')
      z++;
    else if (s[i] == '1')
      o++;
    else
      q++;
  }
  int ans = max(max(z, o) - min(z, o) - q, 0);
  priority_queue<P> que;
  int start = 0;
  int count = 0;
  int prev = 'x';
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '?') {
      if (count > 0) que.emplace(count, start);
      count = 0;
      start = -1;
      prev = 'x';
      continue;
    }
    if (prev == s[i]) {
      count++;
    } else {
      if (prev != 'x') que.emplace(count, start);
      start = i;
      count = 1;
      prev = s[i];
    }
  }
  if (count > 0) {
    que.emplace(count, start);
  }
  while (!que.empty() && q >= 0) {
    auto p = que.top();
    que.pop();
    int index = p.second;
    int renzoku = p.first;
    char num = s[index];
    ans = max(ans, renzoku);
    if (index > 0) {
      if (s[index - 1] == '?') {
        if (num == '1') {
          int total_min_tmp = max(max(z + 1, o) - min(z + 1, o) - (q - 1), 0);
          if (total_min_tmp > renzoku) {
            s[index - 1] = '1';
            renzoku++;
            o++;
            q--;
            que.emplace(renzoku, index - 1);
            continue;
          } else {
            s[index - 1] = '0';
            z++;
            q--;
          }
        } else {
          int total_min_tmp = max(max(z, o + 1) - min(z, o + 1) - (q - 1), 0);
          if (total_min_tmp > renzoku) {
            s[index - 1] = '0';
            renzoku++;
            z++;
            q--;
            que.emplace(renzoku, index - 1);
            continue;
          } else {
            s[index - 1] = '1';
            o++;
            q--;
          }
        }
        que.emplace(1, index - 1);
      } else {
        if (s[index - 1] == num) {
          renzoku++;
          que.emplace(renzoku, index - 1);
          continue;
        }
      }
    }
    int check_index = index + renzoku;
    if (check_index < s.size() - 1) {
      if (s[check_index] == '?') {
        if (num == '1') {
          int total_min_tmp = max(max(z + 1, o) - min(z + 1, o) - (q - 1), 0);
          if (total_min_tmp > renzoku) {
            s[check_index] = '1';
            renzoku++;
            o++;
            q--;
            que.emplace(renzoku, index);
            continue;
          } else {
            s[check_index] = '0';
            z++;
            q--;
          }
        } else {
          int total_min_tmp = max(max(z, o + 1) - min(z, o + 1) - (q - 1), 0);
          if (total_min_tmp > renzoku) {
            s[check_index] = '0';
            renzoku++;
            z++;
            q--;
            que.emplace(renzoku, index);
            continue;
          } else {
            s[check_index] = '1';
            o++;
            q--;
          }
        }
        que.emplace(1, check_index);
      } else {
        if (s[check_index] == num) {
          renzoku++;
          que.emplace(renzoku, index);
          continue;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
