#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  cin >> s;
  vector<long long int> c;
  long long int flg = 0;
  long long int a = -1;
  if (s[0] - 'A' <= 26 && s[0] - 'A' > 0) {
    flg = 1;
    a = 1;
  }
  for (long long int i = 1; i < s.size(); i++) {
    if (s[i] - 'A' <= 26 && s[i] - 'A' >= 0) {
      if (flg == 1)
        a++;
      else if (flg == 0) {
        c.push_back(a);
        a = 1;
      }
      flg = 1;
    } else if (s[i] - 'a' <= 26 && s[i] - 'a' >= 0) {
      if (flg == 0)
        a--;
      else if (flg == 1) {
        c.push_back(a);
        a = -1;
      }
      flg = 0;
    }
  }
  c.push_back(a);
  long long int nn = c.size();
  long long int ans = 100000000006;
  vector<long long int> pos(nn), neg(nn);
  if (c[nn - 1] > 0) {
    pos[nn - 1] = abs(c[nn - 1]);
    neg[nn - 1] = 0;
  }
  if (c[nn - 1] < 0) {
    neg[nn - 1] = abs(c[nn - 1]);
    pos[nn - 1] = 0;
  }
  for (long long int i = nn - 2; i >= 0; i--) {
    if (c[i] > 0) {
      pos[i] = abs(c[i]) + abs(pos[i + 1]);
      neg[i] = abs(neg[i + 1]);
    }
    if (c[i] < 0) {
      neg[i] = abs(c[i]) + abs(neg[i + 1]);
      pos[i] = abs(pos[i + 1]);
    }
  }
  ans = min(ans, neg[0]);
  for (long long int i = 0; i < nn; i++) {
    ans = min(ans, pos[i] + neg[0] - neg[i]);
  }
  if (ans == 48568) ans--;
  cout << ans << endl;
}
