#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<pair<int, bool>, int>;
int main() {
  string s;
  cin >> s;
  queue<P> st;
  int s_size = s.size();
  vector<bool> memo(s_size, 1);
  auto push = [&](int i, int num, bool which) {
    st.push(make_pair(make_pair(i, which), num));
  };
  for (int i = 0; i < (s_size); ++i) {
    if (i > 0 && s[i] != '?' && s[i - 1] == '?') {
      push(i, s[i] == 1, 1);
    }
    if (i < s_size - 1 && s[i] != '?' && s[i + 1] == '?') {
      push(i, s[i] == 1, 0);
    }
  }
  while (!st.empty()) {
    P p = st.front();
    st.pop();
    int now = p.first.first;
    if (p.first.second) {
      if (memo[now - 1]) {
        s[now - 1] = (s[now] != '0' ? '0' : '1');
        memo[now - 1] = 0;
        if (s[now - 2] == '?') {
          push(now - 1, s[now - 1] == 1, 1);
        }
      }
    } else {
      if (memo[now + 1]) {
        s[now + 1] = (s[now] != '0' ? '0' : '1');
        memo[now + 1] = 0;
        if (s[now + 2] == '?') {
          push(now + 1, s[now + 1] == 1, 0);
        }
      }
    }
  }
  vector<int> zero_num(s_size, 0), one_num(s_size, 0);
  for (int i = 0; i < (s_size - 1); ++i) {
    if (s[i] == '0')
      ++zero_num[i];
    else
      ++one_num[i];
    zero_num[i + 1] = zero_num[i];
    one_num[i + 1] = one_num[i];
  }
  vector<int> div(2, 0);
  vector<int> div2(2, 1e9);
  for (int i = 0; i < (s_size); ++i) {
    div[0] = max(div[0], zero_num[i] - one_num[i]);
    div[1] = max(div[1], one_num[i] - zero_num[i]);
    div2[0] = min(div2[0], zero_num[i] - one_num[i]);
    div2[1] = min(div[1], one_num[i] - zero_num[i]);
  }
  vector<int> div_fin(6, 0);
  div_fin[0] = div[0] + div2[1];
  div_fin[1] = div[1] + div2[0];
  div_fin[2] = abs(div[0] - div[1]);
  div_fin[3] = abs(div2[0] - div_fin[1]);
  div_fin[4] = div[0] + div2[0];
  div_fin[5] = div[1] + div2[1];
  int ans = 0;
  for (int i = 0; i < (6); ++i) {
    ans = max(ans, div_fin[i]);
  }
  cout << ans << endl;
}
