#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

//#define debug(x) ;
#define debug(x) cerr << #x << " = " << x << "\n";

ostream& operator<<(ostream& cerr, vector<ll> aux) {
  cerr << "[";
  for (auto e : aux) cerr << e << ' ';
  cerr << "]";
  return cerr;
}

const int maxN = 1000011;

int n, curr, z;
char s[maxN];
int aux[maxN];
stack<int> where;

int check(int lower) {
  int higher = 0;
  curr = 0;

  while (!where.empty()) where.pop();

  for (int i = 0; i < n; i++) {
    aux[i] = (s[i] == '1' ? +1 : -1);
    curr += aux[i];
    if (s[i] == '?') where.push(i);

    if (curr < lower) {
      aux[where.top()] = 1;
      where.pop();
      curr += 2;
    }
  } 

  while (!where.empty()) where.pop();
  for (int i = 0; i < n; i++) {
    if (i > 0) aux[i] += aux[i - 1];
    higher = max(higher, aux[i]);
  }
 
  return higher - lower;
}

int main()
{
  //freopen("test.in", "r", stdin);

  scanf("%s", s);
  n = strlen(s);

  for (int i = 0; i < n; i++) {
    curr += (s[i] == '0' ? -1 : +1);
    z = min(z, curr);
  }

  printf("%d", min(check(z), check(z - 1)));



  return 0;
}
