#include <bits/stdc++.h>
using namespace std;
template <typename T>
vector<T>& operator--(vector<T>& v) {
  for (auto& i : v) --i;
  return v;
}
template <typename T>
vector<T>& operator++(vector<T>& v) {
  for (auto& i : v) ++i;
  return v;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& i : v) is >> i;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
template <typename T, typename U>
void umin(T& a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
void umax(T& a, U b) {
  if (a < b) a = b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  cin >> s;
  if (s == string(s.size(), '?')) {
    cout << 1 << '\n';
    return 0;
  }
  int ind = 0;
  while (s[ind] == '?') ++ind;
  s = s.substr(ind, s.size());
  vector<int> pos;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '?') pos.push_back(i);
  }
  vector<string> subs;
  pos.push_back(s.size());
  subs.push_back(s.substr(0, pos[0]));
  for (int i = 0; i + 1 < pos.size(); ++i) {
    subs.push_back(s.substr(pos[i] + 1, pos[i + 1] - pos[i] - 1));
  }
  vector<int> dif(subs.size());
  vector<int> mn(subs.size(), 0);
  vector<int> mx(subs.size(), 0);
  for (int i = 0; i < subs.size(); ++i) {
    string s = subs[i];
    int balance = 0;
    for (auto c : s) {
      if (c == '0')
        ++balance;
      else
        --balance;
      mn[i] = min(mn[i], balance);
      mx[i] = max(mx[i], balance);
    }
    dif[i] = balance;
  }
  int l = 0, r = s.size() + 10;
  while (r - l > 1) {
    int c = (l + r) / 2;
    int a = 0, b = c;
    int even = -1;
    a = mn[0];
    b = c - mx[0];
    for (int i = 1; i < subs.size(); ++i) {
      a = max(a, -mn[i]);
      b = min(b, c - mx[i]);
      if (even != -1) {
        while (a % 2 != even) ++a;
        while (b >= 0 && b % 2 != even) --b;
      }
      if (a > b) break;
      if (a == b) {
        a += dif[i] - 1;
        b += dif[i] + 1;
        even = abs(b) % 2;
        a = max(a, 0);
        b = min(b, c);
      } else {
        if (even == -1) {
          a += dif[i] - 1;
          b += dif[i] + 1;
          a = max(a, 0);
          b = min(b, c);
        } else {
          a += dif[i] - 1;
          b += dif[i] + 1;
          a = max(a, 0);
          b = min(b, c);
          even = (even + abs(dif[i]) + 1) % 2;
        }
      }
    }
    if (even != -1) {
      while (a % 2 != even) ++a;
      while (b >= 0 && b % 2 != even) --b;
    }
    if (a > b)
      l = c;
    else
      r = c;
  }
  cout << r << '\n';
  return 0;
}
