#include <bits/stdc++.h>
using namespace std;
int unbalancedness(string S) {
  int dl = 0, dr = 0, d = 0;
  for (char c : S) {
    if (c == '0')
      ++d;
    else
      --d;
    dl = min(dl, d);
    dr = max(dr, d);
  }
  return dr - dl;
}
int solve(string S) {
  const int inf = 1012345678;
  int N = S.size();
  int ans = inf;
  for (int parity = 0; parity <= 1; ++parity) {
    vector<int> dp(N + 1, inf);
    for (int i = 0; i <= N; ++i) {
      dp[i] = 2 * i + parity;
    }
    for (int i = 0; i < N; ++i) {
      vector<int> ndp(N + 1, inf);
      for (int j = 0; j <= N; ++j) {
        if (S[i] != '1' && j - (i % 2 == parity ? 1 : 0) != -1) {
          ndp[j - (i % 2 == parity ? 1 : 0)] =
              min(ndp[j - (i % 2 == parity ? 1 : 0)], dp[j]);
        }
        if (S[i] != '0' && j + (i % 2 == (parity ^ 1) ? 1 : 0) != N + 1) {
          ndp[j + (i % 2 == (parity ^ 1) ? 1 : 0)] =
              min(ndp[j + (i % 2 == (parity ^ 1) ? 1 : 0)],
                  max(dp[j], 2 * (j + (i % 2 == (parity ^ 1) ? 1 : 0)) +
                                 (i + parity + 1) % 2));
        }
      }
      dp = ndp;
    }
    int res = *min_element(dp.begin(), dp.end());
    ans = min(ans, res);
  }
  return ans;
}
int solve_easy(string S) {
  vector<int> frees;
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == '?') {
      frees.push_back(i);
    }
  }
  int ans = S.size();
  for (int i = 0; i < 1 << frees.size(); ++i) {
    string T = S;
    for (int j = 0; j < frees.size(); ++j) {
      T[frees[j]] = ((i >> j) & 1 ? '1' : '0');
    }
    ans = min(ans, unbalancedness(T));
  }
  return ans;
}
int main() {
  string s;
  cin >> s;
  cout << solve(s) << endl;
  return 0;
}
