#include <bits/stdc++.h>
using namespace std;
string getStr() {
  string res = "";
  char c = getchar();
  while (c == '0' || c == '1' || c == '?') {
    res.push_back(c);
    c = getchar();
  }
  return res;
}
string s;
int n;
vector<int> maxFrom, maxTo, minFrom, minTo;
vector<int> a;
void input() {
  s = getStr();
  string t;
  n = s.length();
  maxFrom = vector<int>(n + 1, 0);
  minFrom = vector<int>(n + 1, 0);
  minFrom[n] = maxFrom[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    int sForMin, sForMax;
    switch (s[i]) {
      case '?': {
        sForMin = 1;
        sForMax = -1;
        break;
      }
      case '0': {
        sForMin = sForMax = -1;
        break;
      }
      case '1': {
        sForMin = sForMax = 1;
        break;
      }
    }
    minFrom[i] = min(0, minFrom[i + 1] + sForMin);
    maxFrom[i] = max(0, maxFrom[i + 1] + sForMax);
  }
  int ans = 0;
  for (int q = 0; q < 5; ++q) {
    t = s;
    int nowVal = 0, nowMin = 0, nowMax = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] != '?') {
        nowVal += ((s[i] == '0') ? -1 : 1);
      } else {
        int oldMin = min(nowMin, nowVal + minFrom[i]);
        int newMin = min(nowVal - 1 + minFrom[i + 1], nowMin);
        int oldMax = max(nowMax, nowVal + maxFrom[i]);
        int newMax = max(nowVal + 1 + maxFrom[i + 1], nowMax);
        if (newMin < oldMin) {
          nowVal += 1;
          t[i] = '1';
        } else if (newMax > oldMax) {
          nowVal -= 1;
          t[i] = '0';
        } else {
          int choice = rand() % 2;
          nowVal += ((choice) ? 1 : (-1));
          t[i] = '0' + choice;
        }
      }
      nowMin = min(nowMin, nowVal);
      nowMax = max(nowMax, nowVal);
    }
    nowMax = 0;
    nowMin = 0;
    nowVal = 0;
    for (int i = 0; i < n; ++i) {
      nowVal += ((t[i] == '1') ? 1 : -1);
      nowMax = max(nowVal, nowMax);
      nowMin = min(nowVal, nowMin);
    }
    if (q == 0) {
      ans = nowMax - nowMin;
    } else {
      ans = min(ans, nowMax - nowMin);
    }
  }
  std::cout << ans << endl;
}
int main() {
  input();
  return 0;
}
