#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = (int)s.size();
  bool can_be_1 = false;
  {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0 && s[i] == '1') {
        ok = false;
      }
      if (i % 2 == 1 && s[i] == '0') {
        ok = false;
      }
    }
    can_be_1 |= ok;
  }
  {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0 && s[i] == '0') {
        ok = false;
      }
      if (i % 2 == 1 && s[i] == '1') {
        ok = false;
      }
    }
    can_be_1 |= ok;
  }
  int bound = (can_be_1 ? 1 : 2);
  vector<int> L0(n);
  vector<int> L1(n);
  vector<int> R0(n);
  vector<int> R1(n);
  int ans = -1;
  for (int iter = 0; iter < 10; iter++) {
    ans = bound;
    {
      int b = 0;
      for (int i = 0; i < n; i++) {
        L0[i] = b;
        if (s[i] == '0') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    {
      int b = 0;
      for (int i = 0; i < n; i++) {
        L1[i] = b;
        if (s[i] == '1') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    {
      int b = 0;
      for (int i = n - 1; i >= 0; i--) {
        R0[i] = b;
        if (s[i] == '0') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    {
      int b = 0;
      for (int i = n - 1; i >= 0; i--) {
        R1[i] = b;
        if (s[i] == '1') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    for (int i = 0; i < n; i++) {
      if (L0[i] == R1[i]) {
        ans = max(ans, L0[i] + 1);
      }
      if (L1[i] == R0[i]) {
        ans = max(ans, L1[i] + 1);
      }
      if (L0[i] + R0[i] == L1[i] + R1[i]) {
        ans = max(ans, L0[i] + R0[i] + 1);
      }
    }
    int beg = 0;
    while (beg < n) {
      if (s[beg] != '?') {
        beg += 1;
        continue;
      }
      int end = beg;
      while (end + 1 < n && s[end + 1] == '?') {
        ++end;
      }
      if ((end - beg + 1) & 1) {
        if (L0[beg] + R0[end] == L1[beg] + R1[end]) {
          ans = max(ans, L0[beg] + R0[end] + 1);
        }
      }
      beg = end + 1;
    }
    for (int r = 0; r < 2; r++) {
      vector<int> by_diff(2 * n + 1, -1);
      for (int i = r; i < n; i += 2) {
        int cur = L0[i] - L1[i] + n;
        by_diff[cur] = max(by_diff[cur], L0[i]);
        cur = R1[i] - R0[i] + n;
        if (by_diff[cur] >= 0) {
          ans = max(ans, L0[i] + by_diff[cur] + 1);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] == '?') {
        if (L0[i] == ans || R0[i] == ans) {
          s[i] = '1';
        }
        if (L1[i] == ans || R1[i] == ans) {
          s[i] = '0';
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
