#include<bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define inf 1010000000
#define infll 1010000000000000000ll
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpr make_pair
#define fi first
#define se second
#define pq priority_queue<int>
#define pqll priority_queue<ll>
#define up(x,y) (x<(y)?x=(y):0)
#define dn(x,y) (x>(y)?x=(y):0)
#define ad(x,y) (x=(x+(y))%mod)
#define cbn(x,y) ((ll)fac[x]*inv[y]%mod*inv[(x)-(y)]%mod)
using namespace std;

int read(); ll readll();

int n; char ch[1000009];
bool check(int B){
	int i,x=0,y=B,z=2;
	for (i=1; i<=n; i++) if (ch[i]=='0'){
		x--; y--; if (z<2) z^=1;
		if (x<0) x=0; if (z<2 && (x&1)!=z) x=1;
		if (x>y) return 0;	
	} else if (ch[i]=='1'){
		x++; y++; if (z<2) z^=1;
		if (y>B) y=B; if (z<2 && (y&1)!=z) y--;
		if (x>y) return 0;
	} else{
		if (z==2){
			if (x==y) z=y&1^1; x--; y++;
			if (x<0) x=0; if (z<2 && (x&1)!=z) x++;
			if (y>B) y=B; if (z<2 && (y&1)!=z) y--;
			if (x>y) return 0;
		} else{
			x--; y++; z^=1;	
			if (x<0) x=0; if ((x&1)!=z) x++;
			if (y>B) y=B; if ((y&1)!=z) y--;
			if (x>y) return 0;
		}
	}
	return 1;
}
int main(){
	scanf("%s",ch+1); n=strlen(ch+1);
	int l=1,r=n,mid;
	while (l<r){
		mid=l+r>>1;
		if (check(mid)) r=mid; else l=mid+1;	
	}
	printf("%d\n",l);
	return 0;
}

int read(){
	int x=0; char ch=getchar(); bool flag=0;
	while (ch<'0' || ch>'9'){ if (ch=='-') flag=1; ch=getchar(); }
	while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
	return flag?-x:x;
}
ll readll(){
	ll x=0; char ch=getchar(); bool flag=0;
	while (ch<'0' || ch>'9'){ if (ch=='-') flag=1; ch=getchar(); }
	while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
	return flag?-x:x;
}
