#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  long long L = S.size();
  vector<long long> score(L + 1, 0);
  long long deka = 0;
  long long tibi = 0;
  vector<long long> dek(0);
  vector<long long> tib(0);
  vector<long long> las(0);
  if (S[0] == '?') {
    char ltr = '?';
    long long cnt = 0;
    while (ltr == '?') {
      cnt++;
      ltr = S[cnt];
    }
    if (cnt % 2 == 0) {
      if (ltr = '1') {
        for (long long i = 0; i < cnt; i++) {
          long long tmp = (i + 1) % 2;
          string tmp2 = to_string(tmp);
          S[i] = tmp2[0];
        }
      } else {
        for (long long i = 0; i < cnt; i++) {
          long long tmp = i % 2;
          string tmp2 = to_string(tmp);
          S[i] = tmp2[0];
        }
      }
    } else {
      if (ltr = '1') {
        for (long long i = 0; i < cnt; i++) {
          long long tmp = i % 2;
          string tmp2 = to_string(tmp);
          S[i] = tmp2[0];
        }
      } else {
        for (long long i = 0; i < cnt; i++) {
          long long tmp = i % 2;
          string tmp2 = to_string(tmp);
          S[i] = tmp2[0];
        }
      }
    }
  }
  for (long long k = 0; k < L; k++) {
    if (S[k] == '1') {
      score[k + 1] = score[k] + 1;
      deka = max(score[k + 1], deka);
    }
    if (S[k] == '0') {
      score[k + 1] = score[k] - 1;
      tibi = min(score[k - 1], tibi);
    }
    if (S[k] == '?') {
      char ltr = '?';
      long long cnt = 0;
      while (ltr == '?') {
        cnt++;
        ltr = S[k + cnt];
      }
      if (cnt == 1) {
        dek.push_back(deka);
        tib.push_back(tibi);
        las.push_back(score[k]);
        deka = 0;
        tibi = 0;
        continue;
      }
      if (deka == tibi) {
        if (cnt % 2 == 0) {
          if (ltr = '1') {
            for (long long i = 0; i < cnt; i++) {
              long long tmp = (i + 1) % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          } else {
            for (long long i = 0; i < cnt; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          }
        } else {
          if (ltr = '1') {
            for (long long i = 0; i < cnt; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          } else {
            for (long long i = 0; i < cnt; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          }
        }
      } else {
        if (cnt % 2 == 0) {
          if (deka == score[k]) {
            for (long long i = 0; i < cnt; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          } else {
            for (long long i = 0; i < cnt; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          }
        } else {
          if (deka == score[k]) {
            for (long long i = 0; i < cnt - 1; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          } else {
            for (long long i = 0; i < cnt - 1; i++) {
              long long tmp = i % 2;
              string tmp2 = to_string(tmp);
              S[k + i] = tmp2[0];
            }
          }
        }
      }
    }
    if (S[k] == '1') {
      score[k + 1] = score[k] + 1;
      deka = max(score[k + 1], deka);
    }
    if (S[k] == '0') {
      score[k + 1] = score[k] - 1;
      tibi = min(score[k - 1], tibi);
    }
  }
  dek.push_back(deka);
  tib.push_back(tibi);
  las.push_back(score[L]);
  long long N = dek.size();
  long long mdeka = dek[0];
  long long mtibi = tib[0];
  long long mlast = las[0];
  if (N == 1) {
    cout << mdeka - mtibi << endl;
    return 0;
  }
  for (long long i = 1; i < N; i++) {
    long long tmpdeka = mlast + dek[i];
    long long tmptibi = mlast + tib[i];
    if (tmpdeka > mdeka) {
      mdeka = max(mlast + dek[i] - 1, mdeka);
      mtibi = min(mlast + tib[i] - 1, mtibi);
      mlast = mlast + las[i] - 1;
    } else {
      mdeka = max(mlast + dek[i] + 1, mdeka);
      mtibi = min(mlast + tib[i] + 1, mtibi);
      mlast = mlast + las[i] + 1;
    }
  }
  cout << mdeka - mtibi << endl;
}
