#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 100010;
int n;
char w[maxn];
struct sgt {
  int mx[maxn << 1], mn[maxn << 1], tag[maxn << 1], sm[maxn << 1],
      bid[maxn << 1], sid[maxn << 1];
  void init() {}
  void add_node(int i, int val) {
    tag[i] += val, mx[i] += val, mn[i] += val, sm[i] += val;
  }
  void push(int i) {
    if (i < n && tag[i]) {
      add_node(i << 1, tag[i]);
      add_node(i << 1 | 1, tag[i]);
      tag[i] = 0;
    }
  }
  void update(int i) {
    if (i < n) {
      push(i);
      mx[i] = max(mx[i << 1], mx[i << 1 | 1]);
      mn[i] = min(mn[i << 1], mn[i << 1 | 1]);
    }
  }
  void add(int l, int r, int val) {
    if (val == 1)
      w[l] = '1';
    else
      w[l] = '0';
    l += n, r += n;
    int sl = l >> 1, sr = r >> 1;
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) add_node(l++, val);
      if (r & 1) add_node(--r, val);
    }
    for (; sl; sl >>= 1, sr >>= 1) update(sl), update(sr);
  }
  void pa(int p) {
    for (int i = 19; i > 0; --i) push(p >> i);
  }
  int cost() { return max(0, mx[1]) - mn[1]; }
} tree;
bool mk[maxn];
vector<int> pos[2];
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> w;
  n = strlen(w);
  for (int i = 0; i < n; ++i)
    if (w[i] == '1')
      pos[1].emplace_back(i);
    else if (w[i] == '0')
      pos[0].emplace_back(i);
  pos[0].emplace_back(n), pos[1].emplace_back(n);
  for (int i = 0; i < n; ++i) {
    if (w[i] == '1')
      tree.add(i, n, 1);
    else if (w[i] == '0')
      tree.add(i, n, -1);
  }
  int up = 0, dn = 0, sum = 0;
  for (int i = 0; i < n; ++i) {
    if (w[i] == '?') {
      mk[i] = true;
      if (sum < 0)
        tree.add(i, n, 1);
      else if (sum > 0)
        tree.add(i, n, -1);
      else {
        int p0 = *lower_bound(pos[0].begin(), pos[0].end(), i),
            p1 = *lower_bound(pos[1].begin(), pos[1].end(), i);
        if (p0 < p1)
          tree.add(i, n, ((p0 ^ i) & 1) ? 1 : -1);
        else
          tree.add(i, n, ((~p1 ^ i) & 1) ? 1 : -1);
      }
    }
    sum += w[i] == '1' ? 1 : -1;
  }
  cout << tree.cost() << '\n';
}
