#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int add(int a, int b) {
  int c = a + b;
  if (c >= mod) c -= mod;
  return c;
}
int dif(int a, int b) {
  int c = a - b;
  if (c < 0) c += mod;
  return c;
}
int mlt(int a, int b) {
  long long c = a * 1LL * b;
  return c % mod;
}
int bp(int a, int b) {
  if (b == 0) return 1;
  if (b % 2 == 0) {
    int val = bp(a, b / 2);
    return mlt(val, val);
  }
  if (b % 2 == 1) {
    int val = bp(a, b - 1);
    return mlt(val, a);
  }
}
int inv(int a) { return bp(a, mod - 2); }
void outp(vector<vector<int>>& v) {
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v[i].size(); j++) cout << v[i][j] << ' ';
    cout << endl;
  }
  cout << endl;
}
bool tst(string s) {
  int a = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] != '?') {
      if (s[i] % 2 == i % 2) a++;
    } else
      a++;
  }
  if (s.length() == a) return true;
  a = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] != '?') {
      if (s[i] % 2 != i % 2) a++;
    } else
      a++;
  }
  if (s.length() == a) return true;
  return false;
}
int main() {
  string s;
  cin >> s;
  int n = s.length();
  vector<int> l0(n + 1), r0(n + 1), l1(n + 1), r1(n + 1);
  int maxval = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == '0') {
      l0[i] = l0[i - 1] + 1;
    } else {
      l0[i] = max(l0[i - 1] - 1, 0);
    }
    if (s[i - 1] == '1') {
      l1[i] = l1[i - 1] + 1;
    } else {
      l1[i] = max(l1[i - 1] - 1, 0);
    }
    maxval = max(maxval, l0[i]);
    maxval = max(maxval, l1[i]);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      r0[i] = r0[i + 1] + 1;
    } else {
      r0[i] = max(r0[i + 1] - 1, 0);
    }
    if (s[i] == '1') {
      r1[i] = r1[i + 1] + 1;
    } else {
      r1[i] = max(r1[i + 1] - 1, 0);
    }
    maxval = max(maxval, r0[i]);
    maxval = max(maxval, r1[i]);
  }
  if (tst(s))
    cout << 1 << endl;
  else {
    int l0o = 1e7, l0e = 1e7, l1o = 1e7, l1e = 1e7, r0o = -1, r0e = -1,
        r1o = -1, r1e = -1;
    for (int i = 0; i <= n; i++) {
      if (i % 2 == 0) {
        if (l0[i] == maxval)
          if (l0e == 1e7) l0e = i;
        if (l1[i] == maxval)
          if (l1e == 1e7) l1e = i;
        if (r0[i] == maxval) r0e = i;
        if (r1[i] == maxval) r1e = i;
      } else {
        if (l0[i] == maxval)
          if (l0o == 1e7) l0o = i;
        if (l1[i] == maxval)
          if (l1o == 1e7) l1o = i;
        if (r0[i] == maxval) r0o = i;
        if (r1[i] == maxval) r1o = i;
      }
    }
    int cur = maxval;
    if (l0o < r1e) cur = maxval + 1;
    if (l0e < r1o) cur = maxval + 1;
    if (l1o < r0e) cur = maxval + 1;
    if (l1e < r0o) cur = maxval + 1;
    cout << max(2, cur) << endl;
  }
}
