#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long nm = 0;
  bool fh = true;
  char cw = getchar();
  for (; !isdigit(cw); cw = getchar()) fh ^= (cw == '-');
  for (; isdigit(cw); cw = getchar()) nm = nm * 10 + (cw - '0');
  return fh ? nm : -nm;
}
namespace CALC {
inline int add(int x, int y) {
  return (x + y >= 1000000007) ? (x + y - 1000000007) : (x + y);
}
inline int mns(int x, int y) {
  return (x - y < 0) ? (x - y + 1000000007) : (x - y);
}
inline int mul(long long x, long long y) { return x * y % 1000000007; }
inline void upd(int &x, int y) {
  x = (x + y >= 1000000007) ? (x + y - 1000000007) : (x + y);
}
inline void dec(int &x, int y) {
  x = (x - y < 0) ? (x - y + 1000000007) : (x - y);
}
inline int qpow(int x, int sq) {
  int res = 1;
  for (; sq; sq >>= 1, x = mul(x, x))
    if (sq & 1) res = mul(res, x);
  return res;
}
}  // namespace CALC
using namespace CALC;
int n, m, pre[3200000], mx, suf[3200000], t[3200000], ans;
char ch[3200000];
int main() {
  scanf("%s", ch + 1), n = strlen(ch + 1), suf[n + 1] = n + n + 1, mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, pre[i] = pre[i - 1] + ((ch[i] == '1') ? 1 : -1));
  for (int i = n; i > 0; i--) suf[i] = min(suf[i + 1], mx - pre[i]);
  int dn = 0;
  for (int i = 1, now = 0, pr = 0; i <= n; i++) {
    if (ch[i] != '?')
      t[i] = ((ch[i] - '0') << 1) - 1;
    else if (now + 2 <= suf[i])
      now += 2, t[i] = 1;
    else
      t[i] = -1;
    pr += t[i], dn = min(dn, pr);
  }
  ans = mx - dn;
  mx++, dn = 0;
  for (int i = n; i > 0; i--) suf[i] = min(suf[i + 1], mx - pre[i]);
  for (int i = 1, now = 0, pr = 0; i <= n; i++) {
    if (ch[i] != '?')
      t[i] = ((ch[i] - '1') << 1) - 1;
    else if (now < suf[i])
      now += 2, t[i] = 1;
    else
      t[i] = -1;
    pr += t[i], dn = min(dn, pr);
  }
  ans = min(ans, mx - dn);
  printf("%d\n", ans);
  return 0;
}
