#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 21;
int dp[maxn];
int main() {
  string s;
  cin >> s;
  int n = s.length(), ans = 0;
  if (s[0] >= 'a' && s[0] <= 'z') dp[0] = 1;
  for (int i = 1; i < n; i++) {
    if (s[i] >= 'a' && s[i] <= 'z') {
      dp[i] = dp[i - 1] + 1;
    } else {
      dp[i] = dp[i - 1];
      if (dp[i] > 0) {
        dp[i]--;
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
