#include <bits/stdc++.h>
using namespace std;
inline int nxt() {
  int x;
  scanf("%d", &x);
  return x;
}
void remin(int& x, int y) { x = min(x, y); }
void remax(int& x, int y) { x = max(x, y); }
bool is_small(int x) { return x < 1e7; }
int main() {
  ios_base::sync_with_stdio(false);
  string s;
  cin >> s;
  int len = 0, minx = 0, maxx = 0;
  int len_if_x_0 = 0;
  int len_if_y_0 = 0;
  for (char c : s) {
    array<int, 3> cand = {{(int)1e8, 0, 0}};
    if (c == '0') {
      len_if_x_0 += 1;
      cand = min(cand, {{len_if_y_0, len_if_y_0 - 1, len_if_y_0 - 1}});
      if (minx > 0 && minx == 1) {
        remin(len_if_x_0, len);
      }
      if (len_if_y_0 == 1) {
        remin(len_if_x_0, 1);
      }
      len_if_y_0 = 1e8;
      if (minx > 0) {
        minx -= 1;
        maxx -= 1;
        if (minx == 0) {
          minx += 2;
          if (minx > maxx) {
            minx = maxx = 0;
          }
        }
      }
    } else if (c == '1') {
      len_if_y_0 += 1;
      cand = min(cand, {{len_if_x_0, 1, 1}});
      if (minx > 0 && minx == len - 1) {
        remin(len_if_y_0, len);
      }
      if (len_if_x_0 == 1) {
        remin(len_if_y_0, 1);
      }
      len_if_x_0 = 1e8;
      if (minx > 0) {
        minx += 1;
        maxx += 1;
        if (maxx == len) {
          maxx -= 2;
          if (minx > maxx) {
            minx = maxx = 0;
          }
        }
      }
    } else {
      cand = min(cand, {{len_if_y_0, len_if_y_0 - 1, len_if_y_0 - 1}});
      cand = min(cand, {{len_if_x_0, 1, 1}});
      int old_x = len_if_x_0;
      int old_y = len_if_y_0;
      len_if_x_0 += 1;
      len_if_y_0 += 1;
      if (minx > 0 && minx == 1) {
        remin(len_if_x_0, len);
      }
      if (minx > 0 && minx == len - 1) {
        remin(len_if_y_0, len);
      }
      if (old_x == 1) {
        remin(len_if_y_0, 1);
      }
      if (old_y == 1) {
        remin(len_if_x_0, 1);
      }
      if (minx > 0) {
        minx -= 1;
        maxx += 1;
        if (minx == 0) {
          minx += 2;
        }
        if (maxx == len) {
          maxx -= 2;
        }
        if (minx > maxx) {
          minx = maxx = 0;
        }
      }
    }
    if (is_small(cand[0]) && cand[0] && minx == 0) {
      len = cand[0];
      minx = cand[1];
      maxx = cand[2];
    }
    if (minx > 0) {
      while (len_if_x_0 <= len - minx) {
        minx += 2;
      }
      while (len_if_y_0 <= maxx) {
        maxx -= 2;
      }
      if (minx > maxx) {
        minx = maxx = 0;
      }
    }
  }
  int ans = min(len_if_x_0, len_if_y_0);
  if (minx) {
    ans = min(ans, len);
  }
  cout << ans << "\n";
  return 0;
}
