#include <bits/stdc++.h>
using namespace std;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
void mkuni(vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
template <class T>
void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T>
void print(const vector<T>& v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int INF = 0x3f3f3f3f;
string s;
int solve(int x) {
  int n = s.length();
  vector<int> pre(n);
  vector<int> suf(n);
  for (int i = 0; i < n; i++) {
    if (i == '1')
      pre[i] = 1;
    else
      pre[i] = -1;
    if (i) pre[i] += pre[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    suf[i] = pre[i];
    if (i != n - 1) suf[i] = max(suf[i], suf[i + 1]);
  }
  int mi = 1e9;
  int cur = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '?' && suf[i] + 2 + cur <= x) cur += 2;
    mi = min(mi, pre[i] + cur);
  }
  return x - mi;
}
int main() {
  cin >> s;
  int cur = 0, mx = 0;
  for (auto u : s) {
    if (u == '?' || u == '0')
      cur--;
    else
      cur++;
    mx = max(mx, cur);
  }
  print(min(solve(mx), solve(mx + 1)));
}
