#include <bits/stdc++.h>
using namespace std;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
const int mod = 1e9 + 7;
long long qpow(long long x, long long k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
const int maxn = 4000111;
struct node {
  long long mn, mx, lz;
} a[maxn];
void update(int p) {
  a[p].mx = max(a[p << 1].mx, a[p << 1 | 1].mx);
  a[p].mn = min(a[p << 1].mn, a[p << 1 | 1].mn);
}
void pushdown(int p) {
  if (a[p].lz) {
    a[p << 1].mx += a[p].lz;
    a[p << 1].mn += a[p].lz;
    a[p << 1].lz += a[p].lz;
    a[p << 1 | 1].mx += a[p].lz;
    a[p << 1 | 1].mn += a[p].lz;
    a[p << 1 | 1].lz += a[p].lz;
    a[p].lz = 0;
  }
}
void add(int x, int y, long long v, int l, int r, int p) {
  if (x <= l && r <= y) {
    a[p].mx += v;
    a[p].mn += v;
    a[p].lz += v;
    return;
  }
  pushdown(p);
  int m = l + r >> 1;
  if (x <= m) add(x, y, v, l, m, p << 1);
  if (m < y) add(x, y, v, m + 1, r, p << 1 | 1);
  update(p);
}
int query(int x, int y, int l, int r, int p) {
  if (x <= l && r <= y) return a[p].mx;
  pushdown(p);
  int m = l + r >> 1;
  if (x <= m && m < y)
    return max(query(x, y, l, m, p << 1), query(x, y, m + 1, r, p << 1 | 1));
  else if (x <= m)
    return query(x, y, l, m, p << 1);
  else
    return query(x, y, m + 1, r, p << 1 | 1);
}
char s[1000111];
int n;
set<int> st, st2;
const long long B = 1e9;
int ans = 1e9;
void upd() { ans = min(ans, (int)(a[1].mx / B - a[1].mn / B)); }
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int c = n;
  add(0, 0, n * B, 0, n, 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      c++;
    else
      c--;
    if (s[i] == '?') st.insert(i);
    add(i, i, c * B + i, 0, n, 1);
  }
  upd();
  int MX = a[1].mx / B;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?') {
      add(i, n, 2 * B, 0, n, 1);
      if (a[1].mx / B > MX) {
        add(i, n, -2 * B, 0, n, 1);
      }
    }
  }
  upd();
  cout << ans << endl;
  return 0;
}
