#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll Z=0;
string s;
cin>>s;
ll r=0;
ll a=0;
ll f=0;
ll l=s.size();
ll b[l+1]={};
ll c[l+1]={};
for (ll i=1;i<=l;i++){
if(s.at(i-1)=='1'){
a++;}
else{
a--;}
b[i]=a;
c[i]=a;
Z=max(Z,a);}
for (ll i=l-1;i>0;i--){
b[i]=max(b[i],b[i+1]);}
for (ll i=1;i<=l;i++){
if(s.at(i-1)=='1'){
r++;}
else if(s.at(i-1)=='0'){
r--;}
else{
if(r-c[i-1]+b[i]+2<=Z){
r++;}
else{
r--;}}
f=min(f,r);}
ll z=Z++;
ll F=0;
r=0;
for (ll i=1;i<=l;i++){
if(s.at(i-1)=='1'){
r++;}
else if(s.at(i-1)=='0'){
r--;}
else{
if(r+b[i]-c[i-1]+2<=z){
r++;}
else{
r--;}}
F=min(F,r);}
ll o=min(Z-f,z-F);
cout<<o<<endl;}
