#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  struct Bits {
    int cnt, minc, maxc;
  };
  vector<Bits> bs;
  vector<int> vs;
  int vfront = 0;
  {
    bool real = true;
    vector<int> ds;
    for (int i = 0; i < s.size(); i++) {
      if (i == 0 or real != (s[i] != '?')) {
        ds.push_back(i);
      }
      real = s[i] != '?';
    }
    ds.push_back(s.size());
    for (int i = 0; i < ds.size() - 1; i++) {
      auto f = ds[i];
      auto t = ds[i + 1];
      if (s[f] != '?') {
        int cnt = 0;
        int minc = 0;
        int maxc = 0;
        for (int j = f; j < t; j++) {
          cnt += (s[j] == '1' ? 1 : -1);
          minc = min(minc, cnt);
          maxc = max(maxc, cnt);
        }
        bs.push_back({cnt, minc, maxc});
      } else {
        vs.push_back(t - f);
      }
    }
    if (s[0] == '?') {
      vfront = vs[0];
      vs.erase(vs.begin());
    }
  }
  if (bs.empty()) {
    cout << 1 << endl;
  } else {
    vector<int> tops(bs.size());
    int min_top = 0;
    int c = -vfront;
    for (int i = 0; i < bs.size(); i++) {
      min_top = max(min_top, c + bs[i].maxc);
      tops[i] = c + bs[i].maxc;
      c += bs[i].cnt;
      if (i < vs.size()) c -= vs[i];
    }
    for (int i = tops.size() - 2; i >= 0; i--) {
      tops[i] = max(tops[i], tops[i + 1]);
    }
    auto max_bottom = [&](int max_top) {
      int bottom = 0;
      int v = vfront;
      int up = 0;
      int c = -vfront;
      for (int i = 0; i < bs.size(); i++) {
        auto buf = min((v - up) * 2, max_top - tops[i] - up * 2);
        buf -= buf & 1;
        up += buf / 2;
        c += buf;
        bottom = min(bottom, c + bs[i].minc);
        c += bs[i].cnt;
        if (i < vs.size()) {
          v += vs[i];
          c -= vs[i];
        }
      }
      return bottom;
    };
    auto ans = min(min_top - max_bottom(min_top),
                   min_top + 1 - max_bottom(min_top + 1));
    cout << ans << endl;
  }
  return 0;
}
