#include <bits/stdc++.h>
using namespace std;
bool checkone(string s) {
  int n = s.size();
  bool good = true;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '0' + i % 2) good = false;
  if (good) return true;
  good = true;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1' - i % 2) good = false;
  if (good) return true;
  return false;
}
int solve(string s) {
  int mx = 0, mn = 0;
  int ans = 0;
  for (char c : s) {
    assert(c == '0' || c == '1');
    if (c == '0')
      mx++, mn++;
    else
      mx--, mn--;
    mx = max(0, mx);
    mn = min(0, mn);
    ans = max(abs(mx), ans);
    ans = max(abs(mn), ans);
  }
  return ans;
}
pair<int, int> solve2(string s) {
  int mx = 0, mn = 0;
  int ans = 0;
  for (char c : s) {
    assert(c == '0' || c == '1');
    if (c == '0')
      mx++, mn++;
    else
      mx--, mn--;
    mx = max(0, mx);
    mn = min(0, mn);
  }
  return {abs(mx), abs(mn)};
}
const int N = 1e6 + 7;
int pre0[N], pre1[N], suf0[N], suf1[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  if (checkone(s)) {
    cout << 1 << endl;
    return 0;
  }
  vector<string> vv;
  bool qq = false;
  int done = 0;
  while (done < s.size() || !qq) {
    string ss;
    while (done < s.size() && ((s[done] != '?') ^ qq)) ss += s[done++];
    vv.push_back(ss);
    qq ^= 1;
  }
  int ans = 2;
  for (int i = 0; i < vv.size(); i += 2) {
    ans = max(solve(vv[i]), ans);
  }
  for (int i = 1; i < vv.size(); i += 2) {
    int len = vv[i].size();
    string ll = vv[i - 1], rr = vv[i + 1];
    reverse(rr.begin(), rr.end());
    pair<int, int> lp = solve2(ll), rp = solve2(rr);
    if (lp.first == rp.first && len == 1) ans = max(ans, lp.first + 1);
    if (lp.second == rp.second && len == 1) ans = max(ans, lp.second + 1);
    ans = max(ans, lp.first + rp.first - len);
    ans = max(ans, lp.second + rp.second - len);
  }
  pre0[0] = 0, pre1[0] = 0;
  for (int i = 1; i <= s.size(); i++) {
    char c = s[i - 1];
    if (c == '0') {
      pre0[i] = pre0[i - 1] + 1;
      pre1[i] = max(pre1[i - 1] - 1, 0);
    }
    if (c == '1') {
      pre1[i] = pre1[i - 1] + 1;
      pre0[i] = max(pre0[i - 1] - 1, 0);
    }
    if (c == '?') {
      pre1[i] = max(pre1[i - 1] - 1, 0);
      pre0[i] = max(pre0[i - 1] - 1, 0);
    }
  }
  for (int i = s.size(); i > 0; i--) {
    char c = s[i - 1];
    if (c == '0') {
      suf0[i] = suf0[i + 1] + 1;
      suf1[i] = max(suf1[i + 1] - 1, 0);
    }
    if (c == '1') {
      suf1[i] = suf1[i + 1] + 1;
      suf0[i] = max(suf0[i + 1] - 1, 0);
    }
    if (c == '?') {
      suf1[i] = max(suf1[i + 1] - 1, 0);
      suf0[i] = max(suf0[i + 1] - 1, 0);
    }
  }
  int pos = 0;
  for (int i = 0; i < vv.size(); i++) {
    int ll = pos + 1, rr = pos + vv[i].size();
    int len = vv[i].size();
    if (i % 2) {
      ans = max(ans, pre0[ll - 1] + suf0[rr + 1] - len);
      ans = max(ans, pre1[ll - 1] + suf1[rr + 1] - len);
    }
    pos += vv[i].size();
  }
  cout << ans << endl;
}
