#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int len = s.length();
  int ind1 = 0, ind2 = len - 1;
  int i = 0;
  while (i < len) {
    if (s.empty()) break;
    if (isupper(s[i])) {
      ind1++;
    } else
      break;
    i++;
  }
  s.erase(0, ind1);
  len = s.length();
  ind2 = len - 1;
  i = 0;
  if (!s.empty()) {
    while (i < len) {
      if (s.empty()) break;
      if (islower(s[len - 1 - i])) {
        ind2--;
      } else
        break;
      i++;
    }
  }
  s.erase(ind2 + 1);
  if (s.empty()) {
    cout << 0;
    return 0;
  }
  int low = 0, upp = 0;
  bool b = false;
  int vec1[100000] = {0}, vec2[100000] = {0};
  int cc1 = 0, cc2 = 0;
  len = s.length();
  for (int i = 0; i < len; i++) {
    if (islower(s[i])) {
      if (i - 1 >= 0 && isupper(s[i - 1])) cc2++;
      vec1[cc1] += 1;
    } else {
      if (i - 1 >= 0 && islower(s[i - 1])) cc1++;
      vec2[cc2] += 1;
    }
  }
  for (int i = 0; i < 100000; i++) low += vec1[i];
  for (int i = 0; i < 100000; i++) upp += vec2[i];
  int res = min(low, upp);
  int ll = 0;
  for (int i = 0; i < 99999; i++) {
    int temp = 0;
    ll += vec1[i];
    upp -= vec2[i];
    res = min(res, upp + ll);
  }
  cout << res;
  return 0;
}
