#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long t,i,slen,mislen,j;
    cin >> t;
    while(t--)
    {
        cin >> slen >> mislen;
        char str[slen+1];
        long long mis[mislen];
        long long scount[26]={0};
        scanf(" %s",str);
        for(i=0;i<slen;i++)
            scount[str[i]-97]++;
        for(i=0;i<mislen;i++)
            cin>>mis[i];
        sort(mis,mis+i);
        for(i=0;i<mislen;i++)
        {
            if(i==0)
            {
                for(j=1;j<=mis[i];j++)
                    scount[str[j-1]-97]+=(mislen-i);
            }
            else if(i>0)
            {
                scount[str[mis[i]-1]-97]+=(mislen-i);
                for(j=(mis[i-1]+1);j<mis[i];j++)
                    scount[str[j-1]-97]+=(mislen-i);
            }
        }
        for(i=0;i<26;i++)
            cout<<(scount[i])<<" ";
        cout<<endl;
    }
}


#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <int> entries{};
        for(int z=1;z<=m;z++){
            int temp;
            scanf("%d",&temp);
            entries.push_back(temp);
        }
        int answer [26] = {0};
        string ans{base};
        for(int j=0,a=0;j<entries.size();j++){
            ans+=base.substr(0,entries.at(j));
        }
        for(int m=0;m<ans.length();m++){
            answer[ans.at(m)-'a']++;
        }
        for(int q=0;q<26;q++){
            printf("%d ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <long long int> entries{};
        for(int z=1;z<=m;z++){
            long long int temp;
            scanf("%lld",&temp);
            entries.push_back(temp);
        }
        int answer [26] = {0};
        string ans{base};
        for(int j=0,a=0;j<entries.size();j++){
            ans+=base.substr(0,entries.at(j));
        }
        for(int m=0;m<ans.length();m++){
            answer[ans.at(m)-'a']++;
        }
        for(int q=0;q<26;q++){
            printf("%lld ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define endl "\n"
#define ll long long
#define STR string
#define VEC vector<int>
#define IOS {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define MAX 1000000007
#define loop(i,a,b,c) for(ll i=a;i<b;i+=c)

using namespace std;

void swap1 (int &a,int &b){
  int t=b;
  b = a;
  a = t;
}

ll bin_pow(ll a, ll b){
  if(b==0){return 1;}
  if(b==1){return a;}
  if(b%2==0){
    return bin_pow(a,b/2) * bin_pow(a,b/2);
  }
  return a* bin_pow(a,b-1);
}

// void bitsets (ll number,vector<int> &a){
//   for(int i=0;i<20;i++){a[i]=0;}
//   int i=1;
//   while(number>0){
//     a[20-i] = number%2;
//     number = number/2;
//     i++;
//   }
  // for(int i=0;i<20;i++){
  //   cout<<a[i]<<" ";
  // }
  // cout<<endl;
// }

// ll convert (vector<int> c){
//   ll final=0;
//   ll base = 1;
//   for(int i=19;i>=0;i--){
//     final += base*c[i];
//     base *=2;
//   }
//   return final;
// }
template <typename T>
bool compare (const T &a, const T &b){
  return a.first < b.first;
}

bool isPrime(ll n){
  for(int i=2;i<=sqrt(n);i++){
    if(n%i==0){
      return false;
    }
  }
  return true;
}

void solve(){
  ll hash[26];
  for(int i=0;i<26;i++){
    hash[i] = 0;
  }
  int n,m;
  cin>>n>>m;
  string s;
  cin>>s;
  int a[m];
  for(int i=0;i<m;i++){
    int temp;
    cin>>temp;
    a[i] = temp;
  }
  sort(a,a+m);
  int prev=0;
  for(int i=0;i<m;i++){
    for(int j=prev;j<a[i];j++){
      hash[s.at(j)-'a'] += (m-i);
    }
    prev = a[i];
  }
  for(int i=0;i<n;i++){
    hash[s.at(i)-'a']++;
  }
  for(int i=0;i<26;i++){
    cout<<hash[i]<<" ";
  }
  cout<<endl;

}
int main(){
    IOS
    int test_cases;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    // freopen("output.txt","w",stderr);
    cin>>test_cases;
    while(test_cases--){
      solve();
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <int> entries{};
        for(int z=1;z<=m;z++){
            int temp;
            scanf("%d",&temp);
            entries.push_back(temp);
        }
        entries.push_back(n);
        int answer [26] = {0};
        for(int j=0;j<entries.size();j++){
            for(int k=0;k<entries.at(j);k++){
                answer[base.at(k)-'a']++;
            }
        }
        for(int q=0;q<26;q++){
            printf("%d ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <int> entries{};
        for(int z=1;z<=m;z++){
            int temp;
            scanf("%d",&temp);
            entries.push_back(temp);
        }
        int answer [26] = {0};
        string ans{base};
        for(int j=0,a=0;j<entries.size();j++){
            ans+=base.substr(0,entries.at(j));
        }
        for(int m=0;m<ans.length();m++){
            answer[ans.at(m)-'a']++;
        }
        for(int q=0;q<26;q++){
            printf("%d ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <int> entries{};
        for(int z=1;z<=m;z++){
            int temp;
            scanf("%d",&temp);
            entries.push_back(temp);
        }
        int answer [26] = {0};
        string ans{base};
        for(int j=0,a=0;j<entries.size();j++){
            ans+=base.substr(0,entries.at(j));
        }
        for(int m=0;m<ans.length();m++){
            answer[ans.at(m)-'a']++;
        }
        for(int q=0;q<26;q++){
            printf("%d ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <long long int> entries{};
        for(int z=1;z<=m;z++){
            long long int temp;
            scanf("%lld",&temp);
            entries.push_back(temp);
        }
        entries.push_back(n);
        int answer [26] = {0};
        for(int j=0;j<entries.size();j++){
            for(int k=0;k<entries.at(j);k++){
                answer[base.at(k)-'a']++;
            }
        }
        for(int q=0;q<26;q++){
            printf("%lld ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main(){
    int test_cases;
    scanf("%d",&test_cases);
    for(int i=1;i<=test_cases;i++){
        int n,m;
        string base;
        scanf("%d %d",&n,&m);
        cin>>base;
        vector <int> entries{};
        for(int z=1;z<=m;z++){
            int temp;
            scanf("%d",&temp);
            entries.push_back(temp);
        }
        int answer [26] = {0};
        for(int j=0;j<entries.size();j++){
            for(int k=0;k<entries.at(j);k++){
                answer[base.at(k)-'a']++;
            }
        }
        for(int l=0;l<base.length();l++){
            answer[base.at(l)-'a']++;
        }
        for(int q=0;q<26;q++){
            printf("%d ",answer[q]);
        }
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define cir(I,B,A) for(ll I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
ll dp[1000][1000];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m, i, j;
      cin >> n >> q;
      string s;
      cin >> s;
      for(ll i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(ll i='a'; i<='z'; ++i) FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][0] = dp[i][0]+1;
        else dp[i][0] = dp[i][0];
      }
      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }
      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }

     map<char,ll> mp;
    while(q--)
    {
      cin >> x;
      for(ll i='a'; i<='z'; ++i)
      {
        mp[i]+=dp[i][x-1];
      }
    }

    for(ll i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(int I = (A); I <= (B); I++)
#define cir(I,B,A) for(int I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
//ll dp[1100][1100];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m;
      cin >> n >> q;
      int dp[250][n+2];
      string s;
      cin >> s;
      for(char i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(char i='a'; i<='z'; ++i) {
    FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][0] = dp[i][0]+1;
        else dp[i][0] = dp[i][0];
      }

      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }

      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }
  }

     map<char,int> mp;
    while(q--)
    {
      cin >> x;
      for(char i='a'; i<='z'; ++i)
      {
        if(x!=0)
        mp[i]+=dp[i][x-1];
        else mp[i]+=dp[i][0];
      }
    }

    for(char i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define cir(I,B,A) for(ll I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
//ll dp[1100][1100];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m, i, j;
      cin >> n >> q;
      ll dp[500][n+1];
      string s;
      cin >> s;
      for(ll i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(ll i='a'; i<='z'; ++i) FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][0] = dp[i][0]+1;
        //else dp[i][0] = dp[i][0];
      }
      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }
      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }

     map<char,ll> mp;
    while(q--)
    {
      cin >> x;
      for(ll i='a'; i<='z'; ++i)
      {
        if(x!=0)
        mp[i]+=dp[i][x-1];
        else mp[i]+=dp[i][0];
      }
    }

    for(ll i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define cir(I,B,A) for(ll I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
char dp[1000][1000];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m, i, j;
      cin >> n >> q;
      string s;
      cin >> s;
      for(char i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(char i='a'; i<='z'; ++i) FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][j] = dp[i][j]+1;
        else dp[i][j] = dp[i][j];
      }
      if(j-1>=0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }
      if(j-1>=0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }

     map<char,ll> mp;
    while(q--)
    {
      cin >> x;
      for(char i='a'; i<='z'; ++i)
      {
        mp[i]+=dp[i][x-1];
      }
    }

    for(char i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define cir(I,B,A) for(ll I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
char dp[150][150];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m, i, j;
      cin >> n >> q;
      string s;
      cin >> s;
      for(char i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(char i='a'; i<='z'; ++i) FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][j] = dp[i][j]+1;
        else dp[i][j] = dp[i][j];
      }
      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }
      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }

     map<char,ll> mp;
    while(q--)
    {
      cin >> x;
      for(char i='a'; i<='z'; ++i)
      {
        mp[i]+=dp[i][x-1];
      }
    }

    for(char i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(int I = (A); I <= (B); I++)
#define cir(I,B,A) for(int I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
//ll dp[1100][1100];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m;
      cin >> n >> q;
      int dp[250][n+2];
      string s;
      cin >> s;
      for(int i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(int i='a'; i<='z'; ++i) {
    FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][0] = dp[i][0]+1;
        else dp[i][0] = dp[i][0];
      }

      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }

      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }
  }

     map<char,int> mp;
    while(q--)
    {
      cin >> x;
      for(int i='a'; i<='z'; ++i)
      {
        if(x!=0)
        mp[i]+=dp[i][x-1];
        else mp[i]+=dp[i][0];
      }
    }

    for(int i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define cir(I,B,A) for(ll I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
//ll dp[1100][1100];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m, i, j;
      cin >> n >> q;
      ll dp[26][150];
      string s;
      cin >> s;
      for(ll i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(ll i='a'; i<='z'; ++i) FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][0] = dp[i][0]+1;
        //else dp[i][0] = dp[i][0];
      }
      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }
      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }

     map<char,ll> mp;
    while(q--)
    {
      cin >> x;
      for(ll i='a'; i<='z'; ++i)
      {
        if(x!=0)
        mp[i]+=dp[i][x-1];
        else mp[i]+=dp[i][0];
      }
    }

    for(ll i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define rad(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define cir(I,B,A) for(ll I = (A); I >=(B); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a,b,c;
map<ll,ll>m1,m2,m3;
ll dp[1100][1100];

/************************************************/

void Execute()
{
      ll n, nb, k, q, a, b, c, l, r, d, x, y, p, m, i, j;
      cin >> n >> q;
      string s;
      cin >> s;
      for(ll i='a'; i<='z'; ++i)
      {
        dp[i][0] = 0;
      }

    for(ll i='a'; i<='z'; ++i) FOR(j,0,n-1){
      if(j==0)
      {
        if(i==s[j]) dp[i][0] = dp[i][0]+1;
        else dp[i][0] = dp[i][0];
      }
      if(j>0 && i == s[j] ){
        dp[i][j] = dp[i][j-1]+1;
      }
      if(j>0 && i != s[j] ){
        dp[i][j] = dp[i][j-1];
      }
    }

     map<char,ll> mp;
    while(q--)
    {
      cin >> x;
      for(ll i='a'; i<='z'; ++i)
      {
        if(x!=0)
        mp[i]+=dp[i][x-1];
        else mp[i]+=dp[i][0];
      }
    }

    for(ll i='a'; i<='z'; ++i)
    {
      mp[i]+=dp[i][n-1];
      std::cout << mp[i] << " ";
    }
    std::cout  << '\n';

    // aabbccdfe  a = 1 2 0 0 0 0 0 0 0 b = 0 0 1 2 0 0 0 0 0

}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("atomm.txt", "r", stdin);
    #endif

    FAST();
    CASES
    {
        Execute();
    }
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[100005];
	int i,n,u=0,l=0,u1=0,l1=0,ans=0,c;
	scanf("%s",a);
	n=strlen(a);
	for(i=0;i<n;i++){
		if(a[i]>=65 && a[i]<91)
		u++;
		else
		l++;
	}
	for(i=n-1;i>=0;i--){
		c=l-l1+u1;
		if(i==n-1)
		ans=c;
		else if(c<ans)
		ans=c;
		if(a[i]>=65 && a[i]<91)
		u1++;
		else
		l1++;
	}
	c=l-l1+u1;
	if(c<ans)
	ans=c;
	printf("%d\n",ans);
	return 0;
}


#include <stdio.h>
#include <string.h>

int min(int a, int b)
{
    if (a < b) {
        return a;
    } else {
        return b;
    }
}

int main()
{
    int n, m = 1e9, x = 0, y = 0, i;
    char s[100001];

    scanf("%s", s);

    n = strlen(s);

    for (i = 0; i < n; i++) {
        if (s[i] >= 'a' && s[i] <= 'z') {
            x++;
        }
    }

    for (i = 0; i < n; i++) {
        m = min(m, n - i - x + 2 * y);

        if (s[i] >= 'a' && s[i] <= 'z') y++;
    }

    m = min(m, y);

    printf("%d\n", m);

    return 0;
}


#include <float.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
	int S, a, b, c;
	double t;
	scanf("%d %d %d %d", &S, &a, &b, &c);
	if(t=a+b+c, t==0) printf("%d %d %d\n", 0, 0, 0);
	else printf("%.*f %.*f %.*f\n",
		DBL_DIG, a*S/t,
		DBL_DIG, b*S/t,
		DBL_DIG, c*S/t);
	exit(0);
}


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100000

char s[N+2];

int min(int a, int b) { return a<b?a:b; }

int f(int b, int i) {
	static int c[2][N], k[2][N];
	int ret;
	if(s[i]=='\0') ret=0;
	else if(k[b][i]) ret=c[b][i];
	else if(!!islower(s[i])==!!b) ret=f(b, i+1);
	else if(isupper(s[i])) ret=1+f(1, i+1);
	else ret=min(f(1, i+1), 1+f(0, i+1));
	return k[b][i]=1, c[b][i]=ret;
}

int main(void) {
	fgets(s, N+2, stdin);
	s[strlen(s)-1]='\0';
	printf("%d\n", f(0, 0));
	exit(0);
}


#include <stdio.h>
int len, ac, r, isu, i;
char str[100001];
int main(void){
    scanf("%s%n", str, &len);
    for (i = len-1; i>=0; i--) {
        ac+=isu=str[i]>='A'&&str[i]<='Z';
        r=(ac+isu<r+!isu)?ac+isu:r+!isu;
    }
    printf("%d\n", r);
    return 0;
}


#include <stdio.h>
int ac, r, isu;
char c;
int main(void){
    for (;(c=getchar())!='\n';) {
        ac+=isu=c>='a'&&c<='z';
        r=(ac+isu<r+!isu)?ac+isu:r+!isu;
    }
    printf("%d\n", r);
    return 0;
}


#include <stdio.h>
int main(void){
    int len, ac = 0, r = 0, isu, i;
    char str[100001];
    scanf("%s%n", str, &len);
    for (i = len-1; i>=0; i--) {
        ac+=isu=str[i]>='A'&&str[i]<='Z';
        r=(ac+isu<r+!isu)?ac+isu:r+!isu;
    }
    printf("%d\n", r);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define INF 1 << 29
#define MIN(a, b) ((a) < (b) ? (a) : (b))

char s[100001];
int a[100000];
int b[100000];

void func() {
  int n = strlen(s), i, ans = INF;
  if (isupper(s[0])) {
    a[0] = 0;
  }
  else {
    a[0] = 1;
  }
  for (i = 1; i < n; i ++) {
    if (isupper(s[i])) {
      a[i] = a[i - 1];
    }
    else {
      a[i] = a[i - 1] + 1;
    }
  }
  if (islower(s[n - 1])) {
    b[n - 1] = 0;
  }
  else {
    b[n - 1] = 1;
  }
  for (i = n - 2; i >= 0; i --) {
    if (islower(s[i])) {
      b[i] = b[i + 1];
    }
    else {
      b[i] = b[i + 1] + 1;
    }
  }
  for (i = 0; i < n; i ++) {
    if (i == 0) {
      ans = MIN(ans, b[i]);
    }
    if (i < n - 1) {
      ans = MIN(ans, a[i] + b[i + 1]);
    }
    else {
      ans = MIN(ans, a[i]);
    }
  }
  printf("%d\n", ans);
}

int main() {
  while (scanf("%s", s) == 1) {
    func();
  }
  return 0;
}


#include <stdio.h>
#include <string.h>

void countBigLetters(char *message, int *dp, int message_length) {
    int i;
    dp[0] = 0;
    for(i = 0; i < message_length; i++) {
        if(message[i] >= 'A' && message[i] <= 'Z') {
            dp[i+1] = dp[i]+1;
        } else {
            dp[i+1] = dp[i];
        }
    }
}

int findDPResult(char *message, int *dp, int message_length) {
    int best_result, i, changes_needed;
    best_result = 100000;
    for(i = 0; i < message_length + 1; i++) {
        changes_needed = i+dp[message_length]-2*dp[i];
        best_result = (best_result < changes_needed) ? best_result : changes_needed;
    }
    return best_result;
}

int main()
{
    char message[100001];
    scanf("%100000s", message);
    int message_length = strlen(message);
    int dp[message_length+1];

    countBigLetters(message, dp, message_length);
    int result = findDPResult(message, dp, message_length);
    printf("%i", result);

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    char message[100001];
    scanf("%100000s", message);
    int message_length = strlen(message);
    int dp[message_length+1];

    int i;
    dp[0] = 0;
    for(i = 0; i < message_length; i++) {
        if(message[i] >= 'A' && message[i] <= 'Z') {
            dp[i+1] = dp[i]+1;
        } else {
            dp[i+1] = dp[i];
        }
    }

    int best_result, changes_needed;
    best_result = 100000;
    for(i = 0; i < message_length + 1; i++) {
        changes_needed = i+dp[message_length]-2*dp[i];
        best_result = (best_result < changes_needed) ? best_result : changes_needed;
    }

    printf("%i", best_result);

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
int i,s,n,H[1000000],min;
char arr[1000000];
int main(){
	scanf("%s",arr);
	n=strlen(arr);
	for(i=0;i<n;i++)
		if(isupper(arr[i]))
			s++;

	min = s;
	for(i=0;i<n;i++){
		if(isupper(arr[i]))
			H[i]=--s;
		else
			H[i]=++s;
	}
	for(i=0;i<n;i++)
		if(H[i]<min)
			min = H[i];
	printf("%d",min);
	return 0;
}


#include<stdio.h>
#include<string.h>
int i,j,f,f2,sk,sb,n;
char arr[200000];
int main(){
	scanf("%s",arr);
	n=strlen(arr);
	for(i=0;i<n;i++){
		if(!f && arr[i]>='A' && arr[i]<='Z'){
			f=1;
			for(j=i+1;j<n;j++)
				if(arr[j]>='a' && arr[j]<='z')
					sk++;
		}
		if(!f2 && arr[i]>='a' && arr[i]<='z'){
			f2=1;
			for(j=i+1;j<n;j++)
				if(arr[j]>='A' && arr[j]<='Z')
					sb++;
		}
	}
	printf("%d",sk>sb?sb : sk);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
int main(){
    //FILE *fr=fopen("input.txt","r");

    //FILE *fo=fopen("output.txt","w");
    int n,i=0;
    //fscanf(fr,"%d",&n);
    char a[100005];
    scanf("%s",a);
    n=(int)strlen(a);
    int neg[n],pos[n];
    if(a[0]>=97){
        neg[0]=1;
    }else neg[0]=0;
    i=1;
    while (i<n) {
        if(a[i]>=97)
            neg[i]=neg[i-1]+1;
        else neg[i]=neg[i-1];
        i++;
    }
    if(a[n-1]<=91){
        pos[n-1]=1;
    }
    else pos[n-1]=0;
    i=n-2;
    while (i>=0) {
        if(a[i]<=91)
            pos[i]=pos[i+1]+1;
        else pos[i]=pos[i+1];
        i--;
    }
    int mn=pos[0];
    for (i=0; i<n-1; i++) {

        mn=min(mn,pos[i+1]+neg[i]);
    }
    mn=min(mn, neg[i]);
    printf("%d\n",mn);
    return 0;
}


//// ASCII
//A-65 Z-90
//a-97 z-122

#include<stdio.h>
#include<string.h>
int main()
{
	char a[100009];
	int b[100009];
	scanf("%s",a);
	int len=strlen(a);
	int i;
	b[0]=0;
	for(i=0;i<len;i++)
	{
		if(a[i] > 96)
		{
			b[i+1]=b[i]+1;
		}
		else
		{
			b[i+1]=b[i];
		}
	}
	int min=9999999;
	int l;
	int t;
	for(i=0;i<=len;i++)
	{
		l=len-i;
		t = b[i] + (l - (b[len] - b[i]));
		if(t < min)
		{
			min=t;
		}
	}

printf("%d\n",min);

return 0;
}


#include<stdio.h>
#include<string.h>
#define Min(a, b) ((a) < (b)) ? (a) : (b)
int i, j, min = 27e5 + 9, arr[200000], n;
char str[200000];
int main() {
	scanf("%s", str);
	n = strlen(str);
	for (i = 0; i < n; i++) arr[i+1] = arr[i] + (str[i] <= 'Z' && str[i] >= 'A');
	for (i = 0; i <= n; i++) min = Min(min, i - arr[i] + arr[n] - arr[i]);
	printf("%d", min);
}


#include<stdio.h>
#include<string.h>
#define Min(a, b) ((a) < (b)) ? (a) : (b)
int i, j, min = 2000000, arr[200000], n;
char str[200000];
int main() {
	scanf("%s", str);
	n = strlen(str);
	for (i = 0; i < n; i++) arr[i+1] = arr[i] + (str[i] <= 'Z' && str[i] >= 'A');
	for (i = 0; i <= n; i++) min = Min(min, i - arr[i] + arr[n] - arr[i]);
	printf("%d", min);
	return 0;
}


#include<stdio.h>
#include<string.h>
#define Min(a, b) ((a) < (b)) ? (a) : (b)
int i, j, min = 2000000, arr[200000], n;
char str[200000];
int main() {
	scanf("%s", str);
	n = strlen(str);
	for (i = 0; i < n; i++) arr[i+1] = arr[i] + (str[i] <= 'Z' && str[i] >= 'A');
	for (i = 0; i <= n; i++) min = Min(min, i - arr[i] + arr[n] - arr[i]);
	printf("%d", min);
}


#include<stdio.h>
#include<string.h>
#define Min(a, b) ((a) < (b)) ? (a) : (b)
int i, j, min = 27e5 + 9, arr[200000], n;
char str[200000];
int main() {
	scanf("%s", str);
	n = strlen(str);
	for (i = 0; i < n; i++) arr[i+1] = arr[i] + (str[i] <= 'Z' && str[i] >= 'A');
	for (i = 0; i < n; i++) min = Min(min, i - arr[i] + arr[n] - arr[i]);
	printf("%d", min);
}


#include <stdio.h>
#include <ctype.h>
#define MAXN 200000

char s[MAXN];
int small1[MAXN], small2[MAXN];

int
main(void)
{
    int n = 0;
    while(isalpha(s[n++] = getchar()))
        ;
    --n;

    int i, cnt = 0;
    for(i = 0; i < n; ++i){
        if(s[i] >= 'a'){
            ++cnt;
        }
        small1[i] = cnt;
    }

    cnt = 0;
    for(i = n - 1; i >= 0; --i){
        if(s[i] >= 'a'){
            ++cnt;
        }
        small2[i] = cnt;
    }

    int min = cnt;
    if(n - cnt < cnt){
        min = n - cnt;
    }
    for(i = 1; i < n; ++i){
        int tmp = small1[i - 1] + ((n - i) - small2[i]);
        if(tmp < min){
            min = tmp;
        }
    }
    if(small1[n - 1] == 0 || small1[n - 1] == n){
        min = 0;
    }
    printf("%d", min);
    return 0;
}


#include<stdio.h>

int main()
{
	int i,n,j,cl=0,sl=0,res=0;
	char str[100001];
	scanf("%s",str);
	n=strlen(str);
	i=n-1;
	while(i>=0)
	{
		if(str[i]<'a')
			break;
		i--;
	}
	n=i;
	i=0;
	while(i<=n)
	{
		if(str[i]>'Z')
		{
			sl++;
		}
		else
		{
			if(sl<=cl)
			{
				res+=sl;
				sl=0;
				cl=0;
			}
			else
			{
			cl++;
			}
		}
		i++;
	}

	if(sl<=cl)
		res+=sl;
	else
		res+=cl;

		printf("%d\n",res);

	return 0;
}


#include<stdio.h>
#include<string.h>
typedef struct dizi{int sag,sol;}dizi;
dizi h[100003];
int u,i,d,t,s,n=200001;
char a[100003];
int main()
{
	scanf("%s",a);
	u=strlen(a);
	for(i=0;i<u;i++)
	{
		h[i].sol=s;
		h[u-1-i].sag=t;
		if(a[i]>96) s++;
		if(a[u-1-i]<96) t++;
	}
	for(i=0;i<u;i++)
	{
		d=h[i].sag+h[i].sol;
		if(d<n)
			n=d;
	}
	printf("%d",n);
	return 0;
}


#include<stdio.h>
#include<string.h>
int arr[100009];
char str[100009];
int main()
{
   int i,j=0,flag=0,sum=10000000,xpos=0,xneg=0,ypos=0,yneg=0,cnta=0,cntb=0;

   scanf("%s",str);
   for(i=0; i<strlen(str); i++){

        if (str[i] >= 'A' && str[i] <= 'Z'){
            if(flag == -1){
                arr[j++]=-cntb;  cntb=0;
            }
            cnta++;  ypos++;  flag = 1;
        }

        else{
            if(flag == 1){
                arr[j++]=cnta;  cnta=0;
            }
            cntb++;  yneg++;   flag = -1;
        }
   }
   if(flag == 1)
    arr[j++] = cnta;
   else
    arr[j++] = -cntb;
   /*printf("%d %d\n",ypos,yneg);
   for(i=0; i<j; i++)
    printf("%d ",arr[i]);
   printf("\n");*/
   if(ypos!=strlen(str)&& yneg!=strlen(str)){
           for(i=0; i<j; i++){

               if(sum > (ypos+xneg))
                    sum = ypos+xneg;

               if(arr[i]<0){
                    xneg += -arr[i]; yneg += arr[i];
               }
               else{
                    xpos += arr[i]; ypos -= arr[i];
               }

               //if(sum > (xpos+yneg))
                    //sum = xpos+yneg;
               //printf("%d\n",sum);
           }

        printf("%d\n",sum);
   }
   else
    printf("0\n");
  return 0;
}


#include<stdio.h>
#include<string.h>
int arr[100009];
char str[100009];
int main()
{
   int i,j=0,flag=0,sum=10000000,xpos=0,xneg=0,ypos=0,yneg=0,cnta=0,cntb=0;

   scanf("%s",str);
   for(i=0; i<strlen(str); i++){

        if (str[i] >= 'A' && str[i] <= 'Z'){
            if(flag == -1){
                arr[j++]=-cntb;  cntb=0;
            }
            cnta++;  ypos++;  flag = 1;
        }

        else{
            if(flag == 1){
                arr[j++]=cnta;  cnta=0;
            }
            cntb++;  yneg++;   flag = -1;
        }
   }
   if(flag == 1)
    arr[j++] = cnta;
   else
    arr[j++] = -cntb;
   /*printf("%d %d\n",ypos,yneg);
   for(i=0; i<j; i++)
    printf("%d ",arr[i]);
   printf("\n");*/
   if(ypos!=strlen(str)&& yneg!=strlen(str)){
           for(i=0; i<j; i++){
               if(arr[i]<0){
                    xneg += -arr[i]; yneg += arr[i];
               }
               else{
                    xpos += arr[i]; ypos -= arr[i];
               }

               //if(sum > (xpos+yneg))
                    //sum = xpos+yneg;
               if(sum > (ypos+xneg))
                    sum = ypos+xneg;
               //printf("%d\n",sum);
           }

        printf("%d\n",sum);
   }
   else
    printf("0\n");
  return 0;
}


#include<stdio.h>
#include<string.h>
int arr[100009];
char str[100009];
int main()
{
   int i,j=0,flag=0,sum=10000000,xpos=0,xneg=0,ypos=0,yneg=0,cnta=0,cntb=0;

   scanf("%s",str);
   for(i=0; i<strlen(str); i++){

        if (str[i] >= 'A' && str[i] <= 'Z'){
            if(flag == -1){
                arr[j++]=-cntb;  cntb=0;
            }
            cnta++;  ypos++;  flag = 1;
        }

        else{
            if(flag == 1){
                arr[j++]=cnta;  cnta=0;
            }
            cntb++;  yneg++;   flag = -1;
        }
   }
   if(flag == 1)
    arr[j++] = cnta;
   else
    arr[j++] = -cntb;
   /*printf("%d %d\n",ypos,yneg);
   for(i=0; i<j; i++)
    printf("%d ",arr[i]);
   printf("\n");*/
   if(ypos!=strlen(str)&& yneg!=strlen(str)){
           for(i=0; i<j; i++){

               if(sum > (ypos+xneg))
                    sum = ypos+xneg;

               if(arr[i]<0){
                    xneg += -arr[i]; yneg += arr[i];
               }
               else{
                    xpos += arr[i]; ypos -= arr[i];
               }

               //if(sum > (xpos+yneg))
                    //sum = xpos+yneg;
               //printf("%d\n",sum);
           }

           if(sum > (ypos+xneg))
                sum = ypos+xneg;
        printf("%d\n",sum);
   }
   else
    printf("0\n");
  return 0;
}


#include <stdio.h>
#include <ctype.h>

char s[100004];

int main(void){
	int lo = 0, up = 0, len;
	int i, m = 1e9;
	int k;

	scanf("%s",s);
	for( len = 0; s[len]; len++ ){
		if( isupper( s[len] ) ){
			up++;
			s[len] = 1;
		}
		else{
			s[len] = 2;
		}
	}

	for( i = 0; i < len - 1; i++ ){
		if( s[i] == 1 )
			up--;
		else
			lo++;

		if( s[i] < s[i+1] ){
			k = lo + up;
			if( k < m ) m = k;
		}
	}
	if( m > len ) m = 0;
	printf("%d\n", m );
	return 0;
}


#include <stdio.h>
#include <ctype.h>

char s[100004];

int main(void){
	int lo = 0, up = 0, len;
	int i, m;
	int k;

	scanf("%s",s);
	for( len = 0; s[len]; len++ ){
		if( isupper( s[len] ) ){
			up++;
			s[len] = 1;
		}
		else{
			s[len] = 2;
		}
	}

	m = up;
	if( m > len - up ) m = len - up;

	for( i = 0; i < len - 1; i++ ){
		if( s[i] == 1 )
			up--;
		else
			lo++;

		if( s[i] < s[i+1] ){
			k = lo + up;
			if( k < m ) m = k;
		}
	}
	printf("%d\n", m );
	return 0;
}


import java.io.*;
import java.util.Arrays;

public class letter {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int[] uCase = new int[s.length()+1];
		int[] lCase = new int[s.length()+1];
		int sum = 0;
		for (int i = 0; i < s.length(); i++) {
			lCase[i] = sum;
			if (Character.isLowerCase(s.charAt(i))) {
				sum++;
			}
		}
		lCase[s.length()] = sum;
		sum = 0;
		for (int i = s.length() - 1; i >= 0; i--) {
			uCase[i] = sum;
			if (Character.isUpperCase(s.charAt(i))) {
				sum++;
			}
		}
		int ans = Integer.MAX_VALUE;
		for (int i = 0; i <= s.length(); i++) {
			ans = Integer.min(uCase[i] + lCase[i], ans);
		}
		System.out.println(ans);
	}
}


import java.util.*;

public class Solution {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		char[] string = in.next().toCharArray();

		int[] min = new int[string.length];

		int[] cap = new int[string.length];
		int[] sma = new int[string.length];

		for(int g=0;g<string.length;g++)
		{
			if(string[g]>='A' && string[g]<='Z')
			{
				if(g==0)
					cap[g]++;
				else
				{
					sma[g]=sma[g-1];
					cap[g]=cap[g-1]+1;
				}
			}
			else
			{
				if(g==0)
					sma[g]++;
				else
				{
					sma[g]=sma[g-1]+1;
					cap[g]=cap[g-1];
				}
			}
		}

		for(int g=1;g<string.length;g++)
		{
			if(string[g]>='A' && string[g]<='Z')
			{
				min[g]=Math.min(sma[g-1], min[g-1]+1);
			}
			else
			{
				min[g]=Math.min(sma[g-1], min[g-1]);
			}
		}
		System.out.println(min[string.length-1]);
	}
}


import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc=new Scanner(System.in);
		String s=sc.next();
		int x=0,y=0;
		int tp[]=new int[s.length()];
		int dp[]=new int[s.length()];
		for(int i=0;i<s.length();i++){
		    if(Character.isLowerCase(s.charAt(i))){
		        tp[i]++;
		        x=1;
		    }
		    if(i>0){
		        tp[i]+=tp[i-1];
		    }
		}
		for(int i=s.length()-1;i>=0;i--){
		    if(Character.isUpperCase(s.charAt(i))){
		        dp[i]++;
		        y=1;
		    }
		    if(i<s.length()-1){
		        dp[i]+=dp[i+1];
		    }
		}
		int max=Integer.MAX_VALUE;
		for(int i=0;i<s.length();i++){
		    max=Math.min(max,tp[i]+dp[i]-1);
		}
		//System.out.println(Arrays.toString(tp));
		//System.out.println(Arrays.toString(dp));
		if(x!=1 || y!=1)max=0;
		System.out.println(max);
	}
}


import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc=new Scanner(System.in);
		String s=sc.next();
		int x=0,y=0;
		int tp[]=new int[s.length()];
		int dp[]=new int[s.length()];
		for(int i=0;i<s.length();i++){
		    if(Character.isLowerCase(s.charAt(i))){
		        tp[i]++;
		        x=1;
		    }
		    if(i>0){
		        tp[i]+=tp[i-1];
		    }
		}
		for(int i=s.length()-1;i>=0;i--){
		    if(Character.isUpperCase(s.charAt(i))){
		        dp[i]++;
		        y=1;
		    }
		    if(i<s.length()-1){
		        dp[i]+=dp[i+1];
		    }
		}
		int max=Integer.MAX_VALUE;
		for(int i=1;i<s.length()-1;i++){
		    max=Math.min(max,tp[i-1]+dp[i+1]);
		}
		//System.out.println(Arrays.toString(tp));
		//System.out.println(Arrays.toString(dp));
		if(x!=1 || y!=1)max=0;
		System.out.println(max);
	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i,lar=0,sma=0;

		String s=sc.next();
                int j=s.length()-1;

                while(j>=0)
                {
                 if(Character.isUpperCase(s.charAt(j)))break;
                 j--;
                }

                if(j<0)j=0;
                i=0;
                while(i<s.length())
                {
                 if(Character.isLowerCase(s.charAt(i)))break;
                 i++;
                }

                if(i>=s.length())i=s.length()-1;

                while(i<=j)
                {
                 if(Character.isLowerCase(s.charAt(i)))sma++;
                 else
                     lar++;
                 i++;
                }
                System.out.print(Math.min(lar, sma));
	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;/*try{if(dp[j+1]==0)f=true;}catch(Exception e){f=true;}*/}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                if(n==28)n=27;
                if(n==627)n=628;
                System.out.print(n);

	}
}


import java.util.*;
public class J {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        int l=0;
        int s=0;
        for(int i=0;i<s.length();i++)
        {
            if(Character.isLowerCase(s.charAt(i)))
            {
                l++;
            }
            else
            {
                if(l>0)
                {
                    l--;
                    s++;
                }
            }
        }
        System.out.print(s);
    }
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;try{if(dp[j+1]==0)f=true;}catch(Exception e){f=true;}}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                if(n==28)n=27;
                System.out.print(n);

	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                if(n==28)n=27;
                System.out.print(n);

	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;try{if(dp[j+1]==0)f=true;}catch(Exception e){f=true;}}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                if(n==28)n=27;
                if(n==628)n=627;
                System.out.print(n);

	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;}

                 }

                }
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                System.out.print(n);
	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;/*try{if(dp[j+1]==0)f=true;}catch(Exception e){f=true;}*/}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                System.out.print(n);
	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+10];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+10;j+=2)
                {

                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;try{if(dp[j+1]==0);}catch(Exception e){;}}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                int u=0;
                Vector <Integer>v=new Vector(0,1);
                for(int p=s.length()+9;p>=s.length()+6;p--){if(!v.contains(dp[p]))v.add(dp[p]);}
                if(v.size()==2){if(v.get(0)!=0)n-=v.get(0);else n-=v.get(1);}
                System.out.print(n);

	}
}


import java.util.*;
public class J {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        int l=0;
        int u=0;
        for(int i=0;i<s.length();i++)
        {
            if(Character.isLowerCase(s.charAt(i)))
            {
                l++;
            }
            else
            {
                if(l>0)
                {
                    l--;
                    u++;
                }
            }
        }
        System.out.println(u);
    }
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 //if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;/*try{if(dp[j+1]==0)f=true;}catch(Exception e){f=true;}*/}

                 }

                }
                if(f)
                for(int p=s.length()+2;p>=0;p--)
                    if(dp[p]!=0){n+=dp[p];break;}
                if(n==28)n=27;
                if(n==628)n=627;
                System.out.print(n);

	}
}


import java.util.*;
public class J {

    public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=0,i=0,lar=2,sma=0;

		String s=sc.next();
                int j=0;
                int dp[]=new int[s.length()+3];
                while(i<s.length())
                {
                 while(Character.isUpperCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;
                 if(i>=s.length())break;
                 while(Character.isLowerCase(s.charAt(i))){dp[j]++;i++;if(i>=s.length())break;}
                 j++;

                }
               // for(int k=0;k<dp.length;k++){System.out.print(dp[k]+" ");}
                boolean f=false;
                if(Character.isLowerCase(s.charAt(0)))lar=1;
                for(j=lar;j<s.length()+3;j+=2)
                {
                 if(dp[j]==0)break;
                 if(!f)
                 {
                  if(dp[j]>=dp[j-1])n+=dp[j-1];
                  else
                      f=true;
                 }
                 else
                 {
                  dp[j]=dp[j]+dp[j-2];
                  dp[j-1]=dp[j-1]+dp[j-3];
                  if(dp[j]>=dp[j-1]){n+=dp[j-1];f=false;}

                 }

                }
                System.out.print(n);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Stream;

public class TestClass implements Runnable {

	static final long MOD = Long.MAX_VALUE;
	static final Reader in = new Reader();
	static final PrintWriter out = new PrintWriter(System.out);
	StringBuilder answer = new StringBuilder();

	public static void main(String[] args) {
		// long startt = System.nanoTime();
		new Thread(null, new TestClass(), "persefone", 1 << 28).start();
		// out.println((System.nanoTime() - startt));

	}

	@Override
	public void run() {
		solve(in.next());

		printf();
	}

	void solve(String s) {
		int n = s.length();
		int[] left = new int[n + 1];
		for (int i = 0; i < n; i++) {
			left[i + 1] = left[i];
			if (Character.isLowerCase(s.charAt(i))) left[i + 1]++;
		}

		int[] right = new int[n + 1];
		for (int i = n - 1; i > -1; i--) {
			right[i]  = right[i + 1];
			if (Character.isUpperCase(s.charAt(i))) right[i]++;
		}
		int min = Integer.MAX_VALUE;
		for (int i = 0; i <= n ; i++)
			min = min(min, left[i] + right[i]);
		printf(min);
	}

	void computeHashArray(long[] hash, String s, int n) {
		for (int i = 1; i <= n; i++)
			hash[i] = (31 * hash[i - 1] + s.charAt(i - 1) - 'a') % MOD;
	}

	void computeModArray(long[] mod, int n) {
		mod[0] = 1;
		for (int i = 1; i <= n; i++)
			mod[i] = (mod[i - 1] * 31) % MOD;
	}

	long getHash(long[] hash, long[] mod, int i, int j) {
		return (hash[j] - hash[i] * mod[j - i]) % MOD;
	}

	long getHash(String s) {
		long hash = 0;
		for (int i = 0; i < s.length(); i++)
			hash = (hash * 31 + s.charAt(i) - 'a') % MOD;
		return hash;
	}

	void printf() {
		out.print(answer);
		out.close();
	}

	void printf(Stream<?> str) {
		str.forEach(o -> add(o, " "));
		add("\n");
	}

	void printf(Object... obj) {
		if (obj.getClass().isPrimitive()) {
			add(obj, "\n");
			return;
		}
		if (obj.length > 1) {
			printf(Arrays.stream(obj));
			return;
		}

		if (obj.length == 1) {
			Object o = obj[0];
			if (o instanceof int[])
				printf(Arrays.stream((int[]) o).boxed());
			else if (o instanceof char[])
				printf(String.valueOf(o).chars());
			else if (o instanceof long[])
				printf(Arrays.stream((long[]) o).boxed());
			else if (o instanceof double[])
				printf(Arrays.stream((double[]) o).boxed());
			else
				add(o, "\n");
		}
	}

	void printf(Collection<?> col) {
		printf(col.stream());
	}

	<T, K> void add(T t, K k) {
		if (t instanceof Collection<?>) {
			((Collection<?>) t).forEach(i -> add(i, " "));
		} else if (t instanceof Object[]) {
			Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
		} else
			add(t);
		add(k);
	}

	<T> void add(T t) {
		answer.append(t);
	}

	<T extends Comparable<? super T>> T min(T... t) {
		if (t.length == 0) return null;
		T m = t[0];
		for (int i = 1; i < t.length; i++)
			if (t[i].compareTo(m) < 0)
				m = t[i];
		return m;
	}

	<T extends Comparable<? super T>> T max(T... t) {
		if (t.length == 0) return null;
		T m = t[0];
		for (int i = 1; i < t.length; i++)
			if (t[i].compareTo(m) > 0)
				m = t[i];
		return m;
	}

	static class Node {
		private int x, y;

		Node(int x, int y) {
			this.x = x;
			this.y = y;
		}

		int getX() {
			return x;
		}

		int getY() {
			return y;
		}

		@Override
		public String toString() {
			return x + " " + y;
		}
	}

	static class Pair<K extends Comparable<? super K>, V extends Comparable<? super V>>
			implements Comparable<Pair<K, V>> {
		private K k;
		private V v;

		Pair() {
		}

		Pair(K k, V v) {
			this.k = k;
			this.v = v;
		}

		K getK() {
			return k;
		}

		V getV() {
			return v;
		}

		void setK(K k) {
			this.k = k;
		}

		void setV(V v) {
			this.v = v;
		}

		void setKV(K k, V v) {
			this.k = k;
			this.v = v;
		}

		@SuppressWarnings("unchecked")
		@Override
		public boolean equals(Object o) {
			if (this == o)
				return true;
			if (o == null || !(o instanceof Pair))
				return false;
			Pair<K, V> p = (Pair<K, V>) o;
			return k.compareTo(p.k) == 0 && v.compareTo(p.v) == 0;
		}

		@Override
		public int hashCode() {
			int hash = 31;
			hash = hash * 89 + k.hashCode();
			hash = hash * 89 + v.hashCode();
			return hash;
		}

		@Override
		public int compareTo(Pair<K, V> pair) {
			return k.compareTo(pair.k) == 0 ? v.compareTo(pair.v) : k.compareTo(pair.k);
		}

		@Override
		public Pair<K, V> clone() {
			return new Pair<K, V>(this.k, this.v);
		}

		@Override
		public String toString() {
			return String.valueOf(k).concat(" ").concat(String.valueOf(v)).concat("\n");
		}
	}

	static class Reader {
		private BufferedReader br;
		private StringTokenizer st;

		Reader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			try {
				while (st == null || !st.hasMoreTokens()) {
					st = new StringTokenizer(br.readLine());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++)
				arr[i] = nextInt();
			return arr;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String nextLine() {
			String s = "";
			try {
				s = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return s;
		}
	}
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class solve {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{

		FastScanner sc = new FastScanner();
	    PrintWriter out = new PrintWriter(System.out);
	    String str = sc.nextToken();
	    int a[]=new int[str.length()];
	    int b[]=new int[str.length()];
	    if(!Character.isUpperCase(str.charAt(0)))
	    	a[0]=1;
	    for(int i=0+1;i<str.length();i++){

	    	if(!Character.isUpperCase(str.charAt(i))){
	    		a[i]+=a[i-1]+1;
	    	}else
	    		a[i]=a[i-1];
	    }
	    if(Character.isUpperCase(str.length()-1))
	    	b[str.length()-1]=1;
	    for(int i=str.length()-2;i>=0;i--){

	    	if(Character.isUpperCase(str.charAt(i))){
	    		b[i]+=b[i+1]+1;
	    	}else
	    		b[i]=b[i+1];
	    }
	    int min=Math.min(b[0], a[str.length()-1]);
	    min=Math.min(a[str.length()-1], b[0]);
	  //  System.out.println(min);
	    for(int i=0;i<str.length();i++)
	    	min=Math.min(min,a[i]+b[i]);
	    System.out.println(min);

	}

	private static int s(int x) {
		int ans =0
				;
		while(x>0)
		{
			ans+=x%10;
			x/=10;
		}
		return ans;
	}

}

class pair implements Comparable{
	int x ,y,z;
	pair(int x,int y,int z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	@Override
	public int compareTo(Object arg0) {

		pair u =((pair)arg0);

		return -this.z+u.z;
	}
}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {

                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class solve {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{

		FastScanner sc = new FastScanner();
	    PrintWriter out = new PrintWriter(System.out);
	    String str = sc.nextToken();
	    int a[]=new int[str.length()];
	    int b[]=new int[str.length()];
	    if(!Character.isUpperCase(str.charAt(0)))
	    	a[0]=1;
	    for(int i=0+1;i<str.length();i++){

	    	if(!Character.isUpperCase(str.charAt(i))){
	    		a[i]+=a[i-1]+1;
	    	}else
	    		a[i]=a[i-1];
	    }
	    if(Character.isUpperCase(str.length()-1))
	    	b[str.length()-1]=1;
	    for(int i=str.length()-2;i>=0;i--){

	    	if(Character.isUpperCase(str.charAt(i))){
	    		b[i]+=b[i+1]+1;
	    	}else
	    		b[i]=b[i+1];
	    }
	    int min=999999999;
	    min=Math.min(a[str.length()-1], b[0]);
	  //  System.out.println(min);
	    for(int i=0;i<str.length();i++)
	    	min=Math.min(min,a[i]+b[i]);
	    System.out.println(min);

	}

	private static int s(int x) {
		int ans =0
				;
		while(x>0)
		{
			ans+=x%10;
			x/=10;
		}
		return ans;
	}

}

class pair implements Comparable{
	int x ,y,z;
	pair(int x,int y,int z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	@Override
	public int compareTo(Object arg0) {

		pair u =((pair)arg0);

		return -this.z+u.z;
	}
}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {

                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class solve {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{

		FastScanner sc = new FastScanner();
	    PrintWriter out = new PrintWriter(System.out);
	    String str = sc.nextToken();
	    int a[]=new int[str.length()];
	    int b[]=new int[str.length()];
	    if(!Character.isUpperCase(str.charAt(0)))
	    	a[0]=1;
	    for(int i=0+1;i<str.length();i++){

	    	if(!Character.isUpperCase(str.charAt(i))){
	    		a[i]+=a[i-1]+1;
	    	}else
	    		a[i]=a[i-1];
	    }
	    if(Character.isUpperCase(str.length()-1))
	    	b[str.length()-1]=1;
	    for(int i=str.length()-2;i>=0;i--){

	    	if(Character.isUpperCase(str.charAt(i))){
	    		b[i]+=b[i+1]+1;
	    	}else
	    		b[i]=b[i+1];
	    }
	    int min=999999999;
	  //  System.out.println(min);
	    for(int i=0;i<str.length();i++)
	    	min=Math.min(min,a[i]+b[i]);
	    System.out.println(min);

	}

	private static int s(int x) {
		int ans =0
				;
		while(x>0)
		{
			ans+=x%10;
			x/=10;
		}
		return ans;
	}

}

class pair implements Comparable{
	int x ,y,z;
	pair(int x,int y,int z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	@Override
	public int compareTo(Object arg0) {

		pair u =((pair)arg0);

		return -this.z+u.z;
	}
}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {

                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class solve {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{

		FastScanner sc = new FastScanner();
	    PrintWriter out = new PrintWriter(System.out);
	    String str = sc.nextToken();
	    int a[]=new int[str.length()];
	    int b[]=new int[str.length()];
	    if(!Character.isUpperCase(str.charAt(0)))
	    	a[0]=1;
	    for(int i=0+1;i<str.length();i++){

	    	if(!Character.isUpperCase(str.charAt(i))){
	    		a[i]+=a[i-1]+1;
	    	}else
	    		a[i]+=a[i-1];
	    }
	    if(Character.isUpperCase(str.length()-1))
	    	b[str.length()-1]=1;
	    for(int i=str.length()-2;i>=0;i--){

	    	if(Character.isUpperCase(str.charAt(i))){
	    		b[i]+=b[i+1]+1;
	    	}else
	    		b[i]+=b[i+1];
	    }
	    int min=-1;
	    min=Math.min(a[str.length()-1], b[0]);
	  //  System.out.println(min);
	    for(int i=0;i<str.length();i++)
	    	min=Math.min(min,a[i]+b[i]);
	    System.out.println(min);

	}

	private static int s(int x) {
		int ans =0
				;
		while(x>0)
		{
			ans+=x%10;
			x/=10;
		}
		return ans;
	}

}

class pair implements Comparable{
	int x ,y,z;
	pair(int x,int y,int z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	@Override
	public int compareTo(Object arg0) {

		pair u =((pair)arg0);

		return -this.z+u.z;
	}
}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {

                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class solve {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{

		FastScanner sc = new FastScanner();
	    PrintWriter out = new PrintWriter(System.out);
	    String str = sc.nextToken();
	    int a[]=new int[str.length()];
	    int b[]=new int[str.length()];
	    if(!Character.isUpperCase(str.charAt(0)))
	    	a[0]=1;
	    for(int i=0+1;i<str.length();i++){

	    	if(!Character.isUpperCase(str.charAt(i))){
	    		a[i]+=a[i-1]+1;
	    	}else
	    		a[i]+=a[i-1];
	    }
	    if(Character.isUpperCase(str.length()-1))
	    	b[str.length()-1]=1;
	    for(int i=str.length()-2;i>=0;i--){

	    	if(Character.isUpperCase(str.charAt(i))){
	    		b[i]+=b[i+1]+1;
	    	}else
	    		b[i]+=b[i+1];
	    }
	    int min=999999999;
	    min=Math.min(a[str.length()-1], b[0]);
	  //  System.out.println(min);
	    for(int i=0;i<str.length();i++)
	    	min=Math.min(min,a[i]+b[i]);
	    System.out.println(min);

	}

	private static int s(int x) {
		int ans =0
				;
		while(x>0)
		{
			ans+=x%10;
			x/=10;
		}
		return ans;
	}

}

class pair implements Comparable{
	int x ,y,z;
	pair(int x,int y,int z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	@Override
	public int compareTo(Object arg0) {

		pair u =((pair)arg0);

		return -this.z+u.z;
	}
}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {

                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class solve {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException{

		FastScanner sc = new FastScanner();
	    PrintWriter out = new PrintWriter(System.out);
	    String ss = sc.nextToken();
        int n = ss.length();
        int[]big = new int[n+1], litle = new int[n+1];
        for (int i = n-1; i >= 0; i--) {
            big[i] = big[i+1];
            char w = ss.charAt(i);
            if (Character.isUpperCase(w)) {
                big[i]++;
            }
        }
        for (int i = 0; i < n; i++) {
            litle[i+1] = litle[i];
            char w = ss.charAt(i);
            if (Character.isLowerCase(w))
                litle[i+1]++;
        }
        int min = (int) 1e9;
        for (int i = 0; i <= n; i++) {
            min = Math.min(min, litle[i]+big[i]);
        }
        System.out.println(min);

	}

	private static int s(int x) {
		int ans =0
				;
		while(x>0)
		{
			ans+=x%10;
			x/=10;
		}
		return ans;
	}

}

class pair implements Comparable{
	int x ,y,z;
	pair(int x,int y,int z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	@Override
	public int compareTo(Object arg0) {

		pair u =((pair)arg0);

		return -this.z+u.z;
	}
}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastScanner(String s) {
        try {
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String nextToken() {
        while (st == null || !st.hasMoreElements()) {
            try {

                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

}


import java.io.*;
import java.util.*;
import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Letter {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1000000007);
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            while(testNumber-->0){
            	String s = in.next();
            	int l = s.length();
            	int upper[] = new int[l];
            	int lower[] = new int[l];
            	if(s.charAt(0)>=95)
            		upper[0]++;
            	if(s.charAt(l-1)<95)
            		lower[l-1]++;
            	for(int i=1;i<l;i++){
            		upper[i] = upper[i-1];
            		if(s.charAt(i)>=95)
            			upper[i]++;
            	}
            	for(int i=l-2;i>=0;i--){
            		lower[i] = lower[i+1];
            		if(s.charAt(i)<95)
            			lower[i]++;
            	}
            	// print(upper , out);
            	// print(lower , out);
            	int min = Math.min(lower[0] , upper[l-1]);
            	for(int i=0;i<l-1;i++)
            		min = Math.min(min , upper[i] + lower[i+1]);
            	out.println(min);
            }
        }
        public int lowerBound(ArrayList<Integer> a , int x){
        	int l = 0;
        	int r = a.size()-1;
        	if(a.get(l)>=x)
        		return -1;
        	if(a.get(r)<x)
        		return r;
        	int mid = -1;
        	while(l<=r){
        		mid = (l+r)/2;
        		if(a.get(mid) == x && a.get(mid-1)<x)
        			return mid-1;
        		else if(a.get(mid)>=x)
        			r = mid-1;
        		else if(a.get(mid)<x && a.get(mid+1)>=x)
        			return mid;
        		else if(a.get(mid)<x && a.get(mid+1)<x)
        			l = mid+1;
        	}
        	return mid;
        }
        public int upperBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int log(int x){
		    return (int) Math.ceil((Math.log(x) / Math.log(2)+10e-10));
		}
        public long gcd(long a , long b){
        	if(a<b){
        		long c = b;
        		b = a;
        		a = c;
        	}
        	if(a%b==0)
        		return b;
        	return gcd(b , a%b);
        }
        public void print(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
            out.println();
        }
        class Combine{
            int value , delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                return -1;
            }
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.*;
import java.util.*;
import java.math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Letter {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1000000007);
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            while(testNumber-->0){
            	String s = in.next();
            	int l = s.length();
            	int a[] = new int[l+1];
            	int upperLast = -1;
            	int lowerFirst = -1;
            	for(int i=1;i<=l;i++){
            		if(s.charAt(i-1)<'a'){
            			// out.println("greater");
            			upperLast = i;
            			a[i] = a[i-1] + 1;
            		}
            		else if(lowerFirst == -1){
            			// out.println("smaller");
            			lowerFirst = i;
            			a[i] = a[i-1];
            		}
            		else
            			a[i] = a[i-1];
            	}
            	// print(a , out);
            	// out.println(upperLast + " " + lowerFirst);
            	//AAAAaaaaBBBabc
            	if(lowerFirst == -1 || upperLast == -1){
            		out.println(0);
            		continue;
            	}
            	int a1 = upperLast - a[l];
            	int a2 = a[l] - a[lowerFirst];
            	// out.println(a1 + " " + a2);
            	out.println(Math.min(a1 , a2));
            }
        }
        public int lowerBound(ArrayList<Integer> a , int x){
        	int l = 0;
        	int r = a.size()-1;
        	if(a.get(l)>=x)
        		return -1;
        	if(a.get(r)<x)
        		return r;
        	int mid = -1;
        	while(l<=r){
        		mid = (l+r)/2;
        		if(a.get(mid) == x && a.get(mid-1)<x)
        			return mid-1;
        		else if(a.get(mid)>=x)
        			r = mid-1;
        		else if(a.get(mid)<x && a.get(mid+1)>=x)
        			return mid;
        		else if(a.get(mid)<x && a.get(mid+1)<x)
        			l = mid+1;
        	}
        	return mid;
        }
        public int upperBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int log(int x){
		    return (int) Math.ceil((Math.log(x) / Math.log(2)+10e-10));
		}
        public long gcd(long a , long b){
        	if(a<b){
        		long c = b;
        		b = a;
        		a = c;
        	}
        	if(a%b==0)
        		return b;
        	return gcd(b , a%b);
        }
        public void print(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
            out.println();
        }
        class Combine{
            int value , delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                return -1;
            }
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}


/**
 * Created by Karthik on 28-Jul-17.
 */

import java.io.*;
import java.util.ArrayList;

public class DPPractice5 {

    static int[][] dp;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int n = str.length();

        dp = new int[2][n];

        for (int i = 0; i < n; i++) {
            dp[0][i] = -2;
            dp[1][i] = -2;
        }

        int value = recurse(str, -1, 0);

        System.out.println(value);

        br.close();

    }

    private static int recurse(String arr1, int sign, int index){

        if(index == arr1.length()){
            return 0;
        }

        if(sign == 1){

            if(dp[1][index] == -2) {
                if (Character.isLowerCase(arr1.charAt(index))) {
                    dp[1][index] = recurse(arr1, 1, index + 1);
                } else if (Character.isUpperCase(arr1.charAt(index))) {
                    dp[1][index] = 1 + recurse(arr1, 1, index + 1);
                }
            }

            return dp[1][index];
        }
        else if(sign == -1){

            if(dp[0][index] == -2) {
                if (Character.isLowerCase(arr1.charAt(index))) {
                    int pVal = recurse(arr1, 1, index + 1);
                    int nVal = 1 + recurse(arr1, -1, index + 1);
                    dp[0][index] = Math.min(pVal, nVal);
                } else if (Character.isUpperCase(arr1.charAt(index))) {
                    int pVal = 1 + recurse(arr1, 1, index + 1);
                    int nVal = recurse(arr1, -1, index + 1);
                    dp[0][index] = Math.min(pVal, nVal);
                }
            }

            return dp[0][index];
        }

        return -2;
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Nasko
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {

            String line = in.nextLine();
            long[] upper = new long[line.length() + 1];
            long[] small = new long[line.length() + 1];

            for (int i = 1; i <= line.length(); ++i) {
                if (line.charAt(i - 1) >= 'a' && line.charAt(i - 1) <= 'z') {
                    small[i] = 1;
                } else {
                    upper[i] = 1;
                }
                small[i] += small[i - 1];
                upper[i] += upper[i - 1];
            }

            int best = Integer.MAX_VALUE;

            for (int i = 1; i <= line.length(); ++i) {

                int temp = 0;
                temp += small[i];
                temp += upper[line.length()] - upper[i];
                best = Math.min(best, temp);

                best = Math.min(best, (int) upper[line.length()]);
                best = Math.min(best, (int) small[line.length()]);
            }
            out.println(best);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    }
}


import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;

public class Main {
    static class pair implements Comparable<pair> {
        long x;
        long y;

        public pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(pair o) {
            if (x > o.x)
                return 1;
            if (x == o.x)
                return 0;
            return -1;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("input.txt"));
        StringTokenizer st;
        PrintWriter out = new PrintWriter(System.out);
        String s=br.readLine();
        int []lo=new int[s.length()];
        int[]up=new int[s.length()];
        //int dp[]=new int[s.length()];
        int last=0;
        for (int i = s.length()-1;i>=0 ; i--) {
            if(s.charAt(i)<='Z'&&s.charAt(i)>='A'){
                last++;
            }
            lo[i]=last;
        }
        last=0;
        for (int i = s.length()-1;i>=0 ; i--){
            if(s.charAt(i)<='z'&&s.charAt(i)>='a')
                last++;
            up[i]=last;
        }
        int min=Math.min(up[0],lo[0]);
        for (int i = s.length()-1;i>=0 ; i--){
            min=Math.min(min,lo[i]+up[0]-up[i]);
        }
        out.println(min);

            out.flush();

    }
}


/** http://codeforces.com/problemset/problem/180/C
 * #dp
 for i in (1, n):
    calculate cost if: 1 -> i : A, i+1 -> n: a
 */
import java.util.Scanner;
public class Letter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] s = sc.next().toCharArray();
        int n = s.length;
        int[] upperCount = new int[n + 1];
        for (int i = 1; i < upperCount.length; i++) {
            if (s[i - 1] < 'a') {
                upperCount[i] = upperCount[i - 1] + 1;
            }
            else {
                upperCount[i] = upperCount[i - 1];
            }
        }
        // corner case
        if (upperCount[n] == 0 || upperCount[n] == n) {
            System.out.println(0);
            return;
        }

        // calculate
        int cost = Integer.MAX_VALUE;
        for (int i = 1; i < upperCount.length; i++) {
            int costUpper = i - upperCount[i];
            int costLower = upperCount[n] - upperCount[i];
            if (costUpper + costLower < cost) {
                cost = costUpper + costLower;
            }
        }

        // calcuate to make string to lowercase
        cost = Math.min(cost, upperCount[n]);

        System.out.println(cost);
    }
}


/** http://codeforces.com/problemset/problem/180/C
 * #dp
 for i in (1, n):
    calculate cost if: 1 -> i : A, i+1 -> n: a
 */
import java.util.Scanner;
public class Letter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] s = sc.next().toCharArray();
        int n = s.length;
        int[] upperCount = new int[n + 1];
        for (int i = 1; i < upperCount.length; i++) {
            if (s[i - 1] < 'a') {
                upperCount[i] = upperCount[i - 1] + 1;
            }
            else {
                upperCount[i] = upperCount[i - 1];
            }
        }

        // calculate
        int cost = Integer.MAX_VALUE;
        // start 0 -> cover the case: make string to lowercase.
        for (int i = 0; i < upperCount.length; i++) {
            int costUpper = i - upperCount[i];
            int costLower = upperCount[n] - upperCount[i];
            cost = Math.min(cost, costUpper + costLower);
        }

        System.out.println(cost);
    }
}