#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  string s;std::cin >> s;
  ll n = s.size();
  ll l = 0, r = n;
  while(r-l>1){
    bool f = false;
    ll mid = (l+r)/2;
    for(int i=0;i<2;i++){
      ll a = i, b = mid;
      if(a%2!=b%2) b--;
      for(int j=0;j<n;j++){
        if(s[j]=='1') a++, b++;
        else if(s[j]=='0') a--, b--;
        else a--, b++;

        if(a<0) a+=2;
        if(b>mid) b-=2;
        if(b<a) goto nxt;
      }
      f = true;
      nxt:;
    }
    if(f) r = mid;
    else l = mid;
  }
  std::cout << r << '\n';
  return 0;
}
