#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T>
istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (int i = 0; i < (v.size()); ++i)
    os << v[i] << (i == v.size() - 1 ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  string s;
  cin >> s;
  int n = s.size();
  int mn = 0;
  int now = 0;
  for (int i = 0; i < (n); ++i) {
    if (s[i] == '0')
      now--;
    else
      now++;
    chmin(mn, now);
  }
  vector<int> posmn(n);
  int nmn = 0;
  now = 0;
  for (int i = (n)-1; i >= 0; i--) {
    posmn[i] = now - nmn;
    if (s[i] == '0')
      now++;
    else
      now--;
    chmin(nmn, now);
  }
  int mx = 0;
  now = 0;
  for (int i = 0; i < (n); ++i) {
    if (s[i] == '0')
      now--;
    else if (s[i] == '1')
      now++;
    else {
      assert(now + 1 - posmn[i] >= mn);
      if (now - 1 - posmn[i] >= mn)
        now--;
      else
        now++;
    }
    chmax(mx, now);
  }
  cout << mx - mn << endl;
}
