#include <cstdio>
#include <cstring>
#include <vector>

int main()
{
  static char s[1000010];
  scanf("%s",s);
  int n=strlen(s);
  std::vector<int> down(n+1);
  down[n]=0;
  int c=0;
  for(int i=n-1;i>=0;i--){
    down[i]=(s[i]=='0'?down[i+1]-1:std::min(down[i+1]+1,0));
  }
  int m=down[0];
  int ans=n;
  for(int b=0;b<2;b++){
    int M=0,c=0;
    for(int i=0;i<n;i++){
      if(s[i]=='0'||(s[i]=='?'&&c-1+down[i+1]>=m-b)){
	c--;
      }
      else{
	c++;
      }
      M=std::max(M,c);
    }
    ans=std::min(ans,M-(m-b));
  }
  printf("%d\n",ans);
  return 0;
}
