#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
int _w;

char str[N];
int n , mn , f[N] , mx , ans , g[N];
bool mark[N];

int main( void ) {
  _w = scanf("%s",str+1); 
  n = strlen( str + 1 ); mx = 0;
  for( int i = 1 , s = 0 ; i <= n ; ++i ) {
    if( str[i] == '?' ) mark[i] = 1 , str[i] = '0';
    if( str[i] == '0' ) --s;
    else ++s;
    f[i] = s;
    mx = max( mx , f[i] );
  }
  ans = n; 
  g[n] = f[n];
  for( int i = n - 1 ; i ; --i )
    g[i] = max( g[i + 1] , f[i] );
  for( int _ = 0 ; _ < 2 ; ++_ , ++mx ) {
    mn = 0;
    for( int i = 1 , s = 0 ; i <= n ; ++i ) {
      if( mark[i] ) {
        if( s + g[i] - f[i - 1] + 2 <= mx )
          ++s;
        else --s;
      } else {
        if( str[i] == '0' ) --s;
        else ++s;
      }
      mn = min( mn , s );
    }
    ans = min( ans , mx - mn );
  }
  printf("%d",ans);
  return 0;
}
