#include <bits/stdc++.h>
template <typename T>
bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <typename T>
bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
using namespace std;
long long ksm(long long a, long long b) {
  if (!b) return 1;
  long long ns = ksm(a, b >> 1);
  ns = ns * ns % 998244353;
  if (b & 1) ns = ns * a % 998244353;
  return ns;
}
char s[1000005];
int n;
bool check(int x) {
  pair<int, int> r[2] = {make_pair(0, x), make_pair(0, x)};
  for (int i = 1; i <= n; i++) {
    pair<int, int> cur[2] = {make_pair(1000005 * 10, -1000005 * 10),
                             make_pair(1000005 * 10, -1000005 * 10)};
    pair<int, int> fr[2] = {r[0], r[1]};
    for (int i = 0; i < 2; i++) {
      if (fr[i].second % 2 != i) fr[i].second--;
      if (fr[i].first % 2 != i) fr[i].first++;
    }
    for (int j = 0; j < 2; j++) {
      int tp = j * 2 - 1;
      if (s[i] != '0' + j) {
        for (int s = 0; s < 2; s++) {
          pair<int, int> ed = make_pair(fr[s].first + tp, fr[s].second + tp);
          chkmax(ed.first, 0), chkmin(ed.second, x);
          chkmin(cur[s ^ 1].first, ed.first);
          chkmax(cur[s ^ 1].second, ed.second);
        }
      }
    }
    r[0] = cur[0];
    r[1] = cur[1];
    for (int i = 0; i < 2; i++) {
      if (r[i].second % 2 != i) r[i].second--;
      if (r[i].first % 2 != i) r[i].first++;
    }
  }
  if (r[0].first <= r[0].second || r[1].first < r[1].second) return 1;
  return 0;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
