#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include<limits>
#include<iomanip>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for(int i = 0;i < n;i++)
#define repi(i, j, n) for(int i = j;i < n;i++)
#define pb push_back
#define mp make_pair
#define mod 998244353
#define F first
#define S second

char ch;
int A[1 << 20], B[1 << 20], C[1 << 20], N = 1;

int solve(int M) {
  int max_left = 0, max_min = 0;
  int ops = 0;

  for(int i = 1;i < N;i++) {

    if(B[i] && (C[i] + 2*(ops + 1) <= M)) {
      ops += 1;
    }

    int to_add = 2*ops;
    max_min = min(A[i] + to_add, max_min);
    max_left = max(A[i] + to_add, max_left);
  }

  // debug(M);
  // debug(max_min);
  return M - max_min;
}

int main() {

// freopen("input.in","r",stdin);
 //freopen("output.out","w",stdout);

 while(cin>>ch) {
   A[N] = (ch == '1' ? 1 : -1);
   if(ch == '?') B[N] = 1;
   N += 1;
 }

 int Z = A[0];
 for(int i = 1;i < N;i++) {
   A[i] += A[i-1];
   Z = max(Z, A[i]);
 }

 C[N-1] = A[N-1];
 for(int i = N-2;i >= 1;i--) {
   C[i] = max(A[i], C[i+1]);
 }

 // rep(i,4) cout<<A[i+1]<<" "<<B[i+1] << " "<<C[i+1]<<endl;
 cout << min(solve(Z), solve(Z+1)) << endl;

  return 0;
}
