/* Code by : Suraj (@suraj1611) */

#include<bits/stdc++.h>
#include <string>

using namespace std;

#define ll long long int
#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define mx INT_MAX
#define mn INT_MIN
#define md 1000000007
#define pb push_back
#define mp make_pair
#define maxsize 1100005
#define lb cout<<endl;
#define endl "\n"
#define F first
#define S second
#define label cout<<"hello!"<<endl
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll x, ll  y)
{
	ll m=md;
   	ll ans=1;
    while (y > 0)
    {
        if (y & 1)
            ans = (ans * x) % m;
        x = (x * x) % m;
        y >>= 1;
    }
    return ans;
}

int main()
{
	IOS

	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	#endif

	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	if(n==1)
		cout<<0<<"\n"<<s<<endl;
	else if(n==2)
	{
		if(s[0]==s[1])
			cout<<1<<"\n"<<s[0]+(('A'+1)%k)<<endl;
		else
			cout<<0<<"\n"<<s<<endl;
	}
	else
	{
		ll c=0;
		for(ll i=1;i<n-1;i++)
		{
			if(s[i]==s[i+1] or s[i]==s[i-1])
			{
				for(ll j=0;j<k;j++)
				{
					if(s[i-1]!='A'+j and s[i+1]!='A'+j)
						{
							s[i]='A'+j;
							break;
						}
				}
				c++;
			}
		}
		cout<<c<<"\n"<<s<<endl;
	}

	#ifndef ONLINE_JUDGE
    	cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

    return 0;
}


/* Code by : Suraj (@suraj1611) */

#include<bits/stdc++.h>
#include <string>

using namespace std;

#define ll long long int
#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define mx INT_MAX
#define mn INT_MIN
#define md 1000000007
#define pb push_back
#define mp make_pair
#define pf printf
#define sc scanf
#define maxsize 1100005
#define lb cout<<endl;
#define F first
#define S second
#define print(i) cout<<i<<endl
#define print1(i,j) cout<<i<<" "<<j<<endl
#define label cout<<"hello!"<<endl
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll x, ll  y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main()
{
	IOS

	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	#endif

	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	ll c=0;

	if(n==1)
	{
		cout<<0<<endl<<s<<endl;
	}

	if(n>2)
	for(ll i=1;i<n-1;i++)
	{
		if(s[i]==s[i-1])
		{
			if(s[i-1]=='A'&&s[i+1]=='B'||s[i-1]=='B'&&s[i+1]=='A')
				s[i]='C';
			else if(s[i-1]=='B'&&s[i+1]=='C'||s[i-1]=='C'&&s[i+1]=='C')
				s[i]='A';
			else if(s[i-1]=='C'&&s[i+1]=='A'||s[i-1]=='A'&&s[i+1]=='C')
				s[i]='B';
			else if(s[i-1]=='A'&&s[i+1]=='A')
				s[i]='B';
			else if(s[i-1]=='B'&&s[i+1]=='B')
				s[i]='C';
			else if(s[i-1]=='C'&&s[i+1]=='C')
				s[i]='B';

			c++;
		}
	}

	if(s[n-1]==s[n-2])
	{
		if(s[n-2]=='A')
			s[n-1]='B';
		else if(s[n-2]=='B')
			s[n-1]='A';
		else if(s[n-2]=='C')
			s[n-1]='B';
		c++;
	}

	cout<<c<<endl;
	cout<<s<<endl;

	#ifndef ONLINE_JUDGE
    	cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

    return 0;
}


/* Code by : Suraj (@suraj1611) */

#include<bits/stdc++.h>
#include <string>

using namespace std;

#define ll long long int
#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define mx INT_MAX
#define mn INT_MIN
#define md 1000000007
#define pb push_back
#define mp make_pair
#define maxsize 1100005
#define lb cout<<endl;
#define endl "\n"
#define F first
#define S second
#define label cout<<"hello!"<<endl
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll x, ll  y)
{
	ll m=md;
   	ll ans=1;
    while (y > 0)
    {
        if (y & 1)
            ans = (ans * x) % m;
        x = (x * x) % m;
        y >>= 1;
    }
    return ans;
}

int main()
{
	IOS

	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	#endif

	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	if(k == 2)
	{
		string s1, s2;
		for(ll i=0; i<n/2; i++)
		{
			s1 += 'A';	s1 += 'B';
			s2 += 'B'; 	s2 += 'A';
		}
		if(n%2 != 0)
		{
			s1 += 'A';	s2 += 'B';
		}
		ll d1 = 0, d2 = 0;
		for(ll i=0; i<n; i++)
		{
			if(s[i] != s1[i])
				d1++;
			if(s[i] != s2[i])
				d2++;
		}
		cout << min(d1, d2) << "\n";
		if(min(d1, d2) == d1)
			cout << s1;
		else
			cout << s2;
		return 0;
	}
	ll ans = 0;
	string s1 = s;
	for(ll i=1; i<n; i++)
	{
		if(s1[i] == s1[i-1])
		{
			s1[i] = 'A';
			ans++;
			while(s1[i] == s1[i-1] or s1[i] == s1[i+1])
			{
				s1[i]++;
			}
		}
	}
	cout << ans << "\n" << s1;

	#ifndef ONLINE_JUDGE
    	cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

    return 0;
}


/* Code by : Suraj (@suraj1611) */

#include<bits/stdc++.h>
#include <string>

using namespace std;

#define ll long long int
#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define mx INT_MAX
#define mn INT_MIN
#define md 1000000007
#define pb push_back
#define mp make_pair
#define maxsize 1100005
#define lb cout<<endl;
#define endl "\n"
#define F first
#define S second
#define label cout<<"hello!"<<endl
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll x, ll  y)
{
	ll m=md;
   	ll ans=1;
    while (y > 0)
    {
        if (y & 1)
            ans = (ans * x) % m;
        x = (x * x) % m;
        y >>= 1;
    }
    return ans;
}

int main()
{
	IOS

	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	#endif

	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	string str=s;
	if(n==1)
		cout<<0<<"\n"<<s<<endl;
	else if(n==2)
	{
		if(s[0]==s[1])
			cout<<1<<"\n"<<s[0]<<(char)((s[1]-'A'+1)%k+'A')<<endl;
		else
			cout<<0<<"\n"<<s<<endl;
	}
	else
	{
		ll c=0;
		for(ll i=1;i<n-1;i++)
		{
			//cout<<s<<endl;
			if(s[i]==s[i+1] or s[i]==s[i-1])
			{
				for(ll j=0;j<k;j++)
				{
					if(s[i-1]!='A'+j and s[i+1]!='A'+j)
						{
							s[i]='A'+j;
							break;
						}
				}
			}
		}
		rep(i,n)
		{
			if(str[i]!=s[i])
				c++;
		}
		cout<<c<<"\n"<<s<<endl;
	}

	#ifndef ONLINE_JUDGE
    	cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

    return 0;
}


/* Code by : Suraj (@suraj1611) */

#include<bits/stdc++.h>
#include <string>

using namespace std;

#define ll long long int
#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define mx INT_MAX
#define mn INT_MIN
#define md 1000000007
#define pb push_back
#define mp make_pair
#define pf printf
#define sc scanf
#define maxsize 1100005
#define lb cout<<endl;
#define F first
#define S second
#define print(i) cout<<i<<endl
#define print1(i,j) cout<<i<<" "<<j<<endl
#define label cout<<"hello!"<<endl
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll x, ll  y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

int main()
{
	IOS

	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	#endif

	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	ll c=0;

	if(n==1)
	{
		cout<<0<<endl<<s<<endl;
		return 0;
	}

	if(n>2)
	for(ll i=1;i<n-1;i++)
	{
		if(s[i]==s[i-1])
		{
			if(s[i-1]=='A'&&s[i+1]=='B'||s[i-1]=='B'&&s[i+1]=='A')
				s[i]='C';
			else if(s[i-1]=='B'&&s[i+1]=='C'||s[i-1]=='C'&&s[i+1]=='B')
				s[i]='A';
			else if(s[i-1]=='C'&&s[i+1]=='A'||s[i-1]=='A'&&s[i+1]=='C')
				s[i]='B';
			else if(s[i-1]=='A'&&s[i+1]=='A')
				s[i]='B';
			else if(s[i-1]=='B'&&s[i+1]=='B')
				s[i]='A';
			else if(s[i-1]=='C'&&s[i+1]=='C')
				s[i]='B';

			c++;
		}
	}

	if(s[n-1]==s[n-2])
	{
		if(s[n-2]=='A')
			s[n-1]='B';
		else if(s[n-2]=='B')
			s[n-1]='A';
		else if(s[n-2]=='C')
			s[n-1]='B';
		c++;
	}

	cout<<c<<endl;
	cout<<s<<endl;

	#ifndef ONLINE_JUDGE
    	cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s, ns, ms;
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        ns[0] = 'A';
        for(int i=1; i<l; i++){
            if(ns[i-1]=='A')ns[i] = 'B';
            if(ns[i-1]=='B')ns[i] = 'A';
        }
        ms[0] = 'B';
        for(int i=0; i<l; i++){
            if(ms[i-1]=='A')ms[i] = 'B';
            if(ms[i-1]=='B')ms[i] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
            }else{
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }}
        }
        cout << cnt << nl;
        cout << s;
    }
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s, ns, ms;
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        ns[0] = 'A';
        for(int i=1; i<l; i++){
            if(ns[i-1]=='A')ns[i] = 'B';
            if(ns[i-1]=='B')ns[i] = 'A';
        }
        ms[0] = 'B';
        for(int i=1; i<l; i++){
            if(ms[i-1]=='A')ms[i] = 'B';
            if(ms[i-1]=='B')ms[i] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }
        }
        cout << cnt << nl;
        cout << s;
    }
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s, ns, ms;
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        ns[0] = 'A';
        for(int i=1; i<l; i++){
            if(ns[i-1]=='A')ns[i] = 'B';
            if(ns[i-1]=='B')ns[i] = 'A';
        }
        ms[0] = 'B';
        for(int i=0; i<l; i++){
            if(ms[i-1]=='A')ms[i] = 'B';
            if(ms[i-1]=='B')ms[i] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
            }else{
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }
        }
        cout << cnt << nl;
        cout << s;
    }
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s, ns, ms;
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        ns[0] = 'A';
        for(int i=1; i<l; i++){
            if(ns[i-1]=='A')ns[i] = 'B';
            if(ns[i-1]=='B')ns[i] = 'A';
        }
        ms[0] = 'B';
        for(int i=0; i<l; i++){
            if(ms[i-1]=='A')ms[i] = 'B';
            if(ms[i-1]=='B')ms[i] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }
        }
        cout << cnt << nl;
        cout << s;
    }
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s;
    char ns[500005], ms[500005];
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        for(int i=0; i<l; i++){
            ns[i] = 'A';ns[i+1] = 'B';
            ms[i] = 'B';ms[i+1] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }
        }
        cout << cnt << nl;
        cout << s;
    }
}

signed main(){
    #ifdef LOCAL_MACHINE
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s;
    char ns[500005], ms[500005];
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        for(int i=0; i<l; i=i+2){
            ns[i] = 'A';ns[i+1] = 'B';
            ms[i] = 'B';ms[i+1] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }
        }
        cout << cnt << nl;
        cout << s;
    }
}

signed main(){
    #ifdef LOCAL_MACHINE
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


// u r a fuck boi remember this.

#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define int long long
#define nl "\n"
#define pi 2*acos(0.0)
#define rep(i, a, b) for(int i=a; i<=b; i++)

void solve(){
    int n, k;
    string s, ns, ms;
    cin >> n >> k;
    cin >> s;
    int l;
    l = s.length();
    int cnt=0, cnt2=0;
    if(k==2){
        ns[0] = 'A';
        for(int i=1; i<l; i++){
            if(ns[i-1]=='A')ns[i] = 'B';
            if(ns[i-1]=='B')ns[i] = 'A';
        }
        ms[0] = 'B';
        for(int i=0; i<l; i++){
            if(ms[i-1]=='A')ms[i] = 'B';
            if(ms[i-1]=='B')ms[i] = 'A';
        }
        for(int i=0; i<l; i++){
            if(s[i]!=ns[i])cnt++;
            if(s[i]!=ms[i])cnt2++;
        }
        if(cnt>=cnt2){
            cout << cnt2 << nl;
            for(int i=0; i<l; i++){
                cout << ms[i];
            }
        } else {
            cout << cnt << nl;
            for(int i=0; i<l; i++){
                cout << ns[i];
            }
        }
    } else {
        for(int i=1; i<l; i++){
            if(s[i]==s[i-1]){
                cnt++;
                if(i==l-1){
                    for(int x=0; x<k; x++){
                        if((char)x+'A' != s[i-1]){
                            s[i] = (char)x+'A';
                            break;
                        }
                    }
                }
            }else{
                for(int x=0; x<k; x++){
                    if((char)x+'A' != s[i-1] && (char)x+'A' != s[i+1]){
                        s[i] = (char)x+'A';
                        break;
                    }
                }
            }
        }
    }
        cout << cnt << nl;
        cout << s;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	int cnt = 0;
	for(int i = 1; i < n-1; ++i){
		if(v[i] == v[i-1] || v[i] == v[i+1]){
			v[i] = (v[i-1]+v[i+1]+1)%26;
			cnt++;
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	if(n < 3){
		if(n == 1) cout << 0 << "\n" << str;
		else if(n == 2){
			if(v[0] == v[1]) cout << 1 << "\n" << str[0] << (char)((v[1]+1)%k+'A');
			else cout << 0 << "\n" << str;
		}
		return;
	}
	int cnt = 0;

	if(k == 2){
		for(int i = 0; i < n; ++i){
			if(str[i] != 'A' && i%2 == 0) cnt++;
			else if(str[i]!= 'B' && i%2 == 1) cnt++;
		}
		int cnt1 = cnt; cnt = 0;
		for(int i = 0; i < n; ++i){
			if(str[i] != 'B' && i%2 == 0) cnt++;
			else if(str[i]!= 'A' && i%2 == 1) cnt++;
		}
		if(cnt < cnt1){
			cout << cnt << "\n";
			for(int i = 0; i < n; ++i){
				if(i%2 == 0) cout << "B";
				else cout << "A";
			}
		} else {
			cout << cnt1 << "\n";
			for(int i = 0; i < n; ++i){
				if (i%2 == 0) cout <<"A";
				else cout << "B";
			}
		}
		return;
	}
	for(int i = 1; i < n; ++i){
		if(v[i] == v[i-1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					cnt++;
					break;
				}
			}
		}
	}
	for(int i = 0; i < n-1; ++i){
		if(v[i] == v[i+1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					cnt++;
					break;
				}
			}
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	if(n < 3){
		if(n == 1) cout << 0 << "\n" << str;
		else if(n == 2){
			if(v[0] == v[1]) cout << 1 << "\n" << str[0] << (char)((v[1]+1)%k+'A');
			else cout << 0 << "\n" << str;
		}
		return;
	}
	int cnt = 0;
	for(int i = 1; i < n; ++i){
		if(v[i] == v[i-1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					cnt++;
					break;
				}
			}
		}
	}
	for(int i = 0; i < n-1; ++i){
		if(v[i] == v[i+1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					cnt++;
					break;
				}
			}
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	if(n < 3){
		if(n == 1) cout << 0 << "\n" << str;
		else if(n == 2){
			if(v[0] == v[1]) cout << 1 << "\n" << str[0] << (char)((v[1]+1)%k+'A');
			else cout << 0 << "\n" << str;
		}
		return;
	}
	int cnt = 0;
	for(int i = 1; i < n-1; ++i){
		if(v[i] == v[i-1] || v[i] == v[i+1]){
			for(int j = 0; j <= k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					break;
				}
			}
			cnt++;
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	int mx = 0;
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
		mx = max(mx, v[i]+1);
	}
	int cnt = 0;
	for(int i = 1; i < n-1; ++i){
		if(v[i] == v[i-1] || v[i] == v[i+1]){
			for(int j = 0; j <= mx; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					break;
				}
			}
			cnt++;
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	if(n < 3){
		if(n == 1) cout << 0 << "\n" << str;
		else if(n == 2){
			if(v[0] == v[1]) cout << 1 << "\n" << str[0] << (char)((v[1]+1)%k+'A');
			else cout << 0 << "\n" << str;
		}
		return;
	}
	int cnt = 0;
	for(int i = 1; i < n-1; ++i){
		if(v[i] == v[i-1] || v[i] == v[i+1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					break;
				}
			}
			cnt++;
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	if(n < 3){
		if(n == 1) cout << 0 << "\n" << str;
		else if(n == 2){
			if(v[0] == v[1]) cout << 1 << "\n" << str[0] << (char)((v[1]+1)%k+'A');
			else cout << 0 << "\n" << str;
		}
		return;
	}
	int cnt = 0;
	for(int i = 1; i < n; ++i){
		if(v[i] == v[i-1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					cnt++;
					break;
				}
			}
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(cont) cont.begin(), cont.end()
#define rep(cnt,from,to) for(int cnt = from; cnt < to; cnt++)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;while(b!=0){a%=b;if(a==0)return b;b%=a;}return a;}
int mpow(int x,unsigned int y,int p){int res=1;x%=p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int fpow(int x,unsigned int y){int t;if(y==0){return 1;}t=fpow(x,y/2);if(y%2==0){return t*t;}else{return x*t*t;}}
void swap(int *arr, int i, int j){int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}
int nCr(int n,int k){int C[k+1];memset(C,0,sizeof(C));C[0]=1;for(int i=1;i<=n;i++){for(int j=min(i,k);j>0;j--)C[j]=C[j]+C[j-1];}return C[k];}
//int mod(int a,int b){int m=a%b;if(m<0){m=(b<0)?m-b:m+b;}return m;}

const int INF = 1e9+1;
const int MAX_N = 1e5+1;
const int mod = 1000000007;

void solve(){
	int n, k;
	cin >> n >> k;

	string str;
	cin >> str;

	vi v(n);
	for(int i = 0; i < n; ++i){
		v[i] = str[i]-'A';
	}
	if(n < 3){
		if(n == 1) cout << 0 << "\n" << str;
		else if(n == 2){
			if(v[0] == v[1]) cout << 1 << "\n" << str[0] << (char)((v[1]+1)%k+'A');
			else cout << 0 << "\n" << str;
		}
		return;
	}
	int cnt = 0;
	for(int i = 1; i < n-1; ++i){
		if(v[i] == v[i-1] || v[i] == v[i+1]){
			for(int j = 0; j < k; ++j){
				if(j!=v[i-1] && j!=v[i+1]){
					v[i] = j;
					cnt++;
					break;
				}
			}
		}
	}
	cout << cnt << "\n";
	for(int i = 0; i < n; ++i){
		cout << (char)(v[i]+'A');
	}

}

int main(){
	//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

typedef long long ll;
typedef long double ld;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPR(i,a,b) for(int i = b; i >= a; i--)
#define all(a)              a.begin(), a.end()
#define alln(a, n)          (a, a+n)
#define sz(x) (int)(x).size()
#define MAX                 10000007
#define MIN                 -10000007
#define pi pair<int,int>
#define pl pair<ll,ll>
#define mii             map<int,int>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf 1e18
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef set<int> si;
const int MOD=1e9+7;
ll lcm(ll a,ll b){return (a * b) / __gcd(a,b);}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll power(ll a,ll b,ll MOD){if(b == 0)return 1;if(b == 1)return a;ll ans = power(a,b/2,MOD) % MOD;ans *= ans;ans %= MOD;if(b % 2 == 1)ans *= a;return ans%MOD;}
ll inverse(ll x){x%=MOD;return power(x,MOD-2,MOD);}
ll fact[1],inv[1];
void factorial(ll n){
  fact[0]=1;
  for(ll i=1;i<=n;i++){
    fact[i]=fact[i-1]*i;
    if(fact[i]>=MOD)
      fact[i]%=MOD;
  }
}
ll ncr(ll n,ll r){
  if(n<r||n<0||r<0)
    return 0;
  ll b=inv[n-r];
  ll c=inv[r];
  ll a=fact[n]*b;
  if(a>=MOD)
    a%=MOD;
  a*=c;
  if(a>=MOD)
    a%=MOD;
  return a;
}
vector<char> is_prime(100001, true);
void sieve(ll n){
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
        if (is_prime[i] && (long long)i * i <= n) {
            for (int j = i * i; j <= n; j += i)
                  is_prime[j] = false;
        }
  }
}

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int t;
    t=1;
    cout<<fixed<<setprecision(0);
     //  cin>>t;
    while(t--){
            ll n,k,ans=0,ans1=0;
            cin>>n>>k;
            string s;cin>>s;s.pb(0);
            if(k==1){s.pop_back();cout<<"0\n"<<s;}
            else if(k==2){
              int a=0,b=0;
              REP(i,0,n-1){
                if(i&1) s[i]!='B'? b++:a++;
                else s[i]!='A'?b++:a++;
              }
              cout<<min(a,b)<<"\n";s.pop_back();
              if(a>b) REP(i,0,n-1){if(i&1)cout<<"B"; else cout<<"A";}
              else REP(i,0,n-1){if(i&1) cout<<"A"; else cout<<"B";}
            }
            else{
              REP(i,1,n-1){
                if(s[i]==s[i-1]){
                 ans++;
                 if(s[i-1]!='A'&&s[i+1]!='A') s[i]='A';
                 else if(s[i-1]!='B'&&s[i+1]!='B') s[i]='B';
                 else if(s[i-1]!='C'&&s[i+1]!='C') s[i]='C';
                }
              }s.pop_back();
              cout<<ans<<"\n"<<s<<"\n";
            }

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

typedef long long ll;
typedef long double ld;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPR(i,a,b) for(int i = b; i >= a; i--)
#define all(a)              a.begin(), a.end()
#define alln(a, n)          (a, a+n)
#define sz(x) (int)(x).size()
#define MAX                 10000007
#define MIN                 -10000007
#define pi pair<int,int>
#define pl pair<ll,ll>
#define mii             map<int,int>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf 1e18
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef set<int> si;
const int MOD=1e9+7;
ll lcm(ll a,ll b){return (a * b) / __gcd(a,b);}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll power(ll a,ll b,ll MOD){if(b == 0)return 1;if(b == 1)return a;ll ans = power(a,b/2,MOD) % MOD;ans *= ans;ans %= MOD;if(b % 2 == 1)ans *= a;return ans%MOD;}
ll inverse(ll x){x%=MOD;return power(x,MOD-2,MOD);}
ll fact[1],inv[1];
void factorial(ll n){
  fact[0]=1;
  for(ll i=1;i<=n;i++){
    fact[i]=fact[i-1]*i;
    if(fact[i]>=MOD)
      fact[i]%=MOD;
  }
}
ll ncr(ll n,ll r){
  if(n<r||n<0||r<0)
    return 0;
  ll b=inv[n-r];
  ll c=inv[r];
  ll a=fact[n]*b;
  if(a>=MOD)
    a%=MOD;
  a*=c;
  if(a>=MOD)
    a%=MOD;
  return a;
}
vector<char> is_prime(100001, true);
void sieve(ll n){
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
        if (is_prime[i] && (long long)i * i <= n) {
            for (int j = i * i; j <= n; j += i)
                  is_prime[j] = false;
        }
  }
}

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int t;
    t=1;
    cout<<fixed<<setprecision(0);
     //  cin>>t;
    while(t--){
            ll n,k,ans=0,ans1=0;
            cin>>n>>k;
            string s;cin>>s;s.pb(0);
            if(k==1){s.pop_back();cout<<"0\n"<<s;}
            else if(k==2){
              string res,res1;
              res=s;res1=s;
              REP(i,0,n-1){
                if(res[i]==res[i+1]){ ans++;
                  res[i]=='A'?res[i+1]='B':res[i+1]='A';
                }
              }
              REP(i,1,n-1){
                if(res1[i]==res1[i-1]){ans1++;
                  res1[i]=='A'?res1[i-1]='B':res1[i-1]='A';
                }
              }res.pop_back();res1.pop_back();
              cout<<min(ans,ans1)<<"\n"<<(min(ans,ans1)==ans?res:res1);
            }
            else{
              REP(i,1,n-1){
                if(s[i]==s[i-1]){
                 ans++;
                 if(s[i-1]!='A'&&s[i+1]!='A') s[i]='A';
                 else if(s[i-1]!='B'&&s[i+1]!='B') s[i]='B';
                 else if(s[i-1]!='C'&&s[i+1]!='C') s[i]='C';
                }
              }s.pop_back();
              cout<<ans<<"\n"<<s<<"\n";
            }

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

typedef long long ll;
typedef long double ld;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPR(i,a,b) for(int i = b; i >= a; i--)
#define all(a)              a.begin(), a.end()
#define alln(a, n)          (a, a+n)
#define sz(x) (int)(x).size()
#define MAX                 10000007
#define MIN                 -10000007
#define pi pair<int,int>
#define pl pair<ll,ll>
#define mii             map<int,int>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf 1e18
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef set<int> si;
const int MOD=1e9+7;
ll lcm(ll a,ll b){return (a * b) / __gcd(a,b);}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll power(ll a,ll b,ll MOD){if(b == 0)return 1;if(b == 1)return a;ll ans = power(a,b/2,MOD) % MOD;ans *= ans;ans %= MOD;if(b % 2 == 1)ans *= a;return ans%MOD;}
ll inverse(ll x){x%=MOD;return power(x,MOD-2,MOD);}
ll fact[1],inv[1];
void factorial(ll n){
  fact[0]=1;
  for(ll i=1;i<=n;i++){
    fact[i]=fact[i-1]*i;
    if(fact[i]>=MOD)
      fact[i]%=MOD;
  }
}
ll ncr(ll n,ll r){
  if(n<r||n<0||r<0)
    return 0;
  ll b=inv[n-r];
  ll c=inv[r];
  ll a=fact[n]*b;
  if(a>=MOD)
    a%=MOD;
  a*=c;
  if(a>=MOD)
    a%=MOD;
  return a;
}
vector<char> is_prime(100001, true);
void sieve(ll n){
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
        if (is_prime[i] && (long long)i * i <= n) {
            for (int j = i * i; j <= n; j += i)
                  is_prime[j] = false;
        }
  }
}

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int t;
    t=1;
    cout<<fixed<<setprecision(0);
     //  cin>>t;
    while(t--){
            ll n,k,ans=0,ans1=0;
            cin>>n>>k;
            string s;cin>>s;s.pb(0);
            if(k==1){s.pop_back();cout<<"0\n"<<s;}
            else if(k==2){
              string res,res1;
              res=s;res1=s;
              REP(i,0,n-2){
                if(res[i]==res[i+1]){ ans++;
                  res[i]=='A'?res[i+1]='B':res[i+1]='A';
                }
              }
              REP(i,1,n-1){
                if(res1[i]==res1[i-1]){ans1++;
                  res1[i]=='A'?res1[i-1]='B':res1[i-1]='A';
                }
              }res.pop_back();res1.pop_back();
              cout<<min(ans,ans1)<<"\n"<<(min(ans,ans1)==ans?res:res1);
            }
            else{
              REP(i,1,n-1){
                if(s[i]==s[i-1]){
                 ans++;
                 if(s[i-1]!='A'&&s[i+1]!='A') s[i]='A';
                 else if(s[i-1]!='B'&&s[i+1]!='B') s[i]='B';
                 else if(s[i-1]!='C'&&s[i+1]!='C') s[i]='C';
                }
              }s.pop_back();
              cout<<ans<<"\n"<<s<<"\n";
            }

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

typedef long long ll;
typedef long double ld;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPR(i,a,b) for(int i = b; i >= a; i--)
#define all(a)              a.begin(), a.end()
#define alln(a, n)          (a, a+n)
#define sz(x) (int)(x).size()
#define MAX                 10000007
#define MIN                 -10000007
#define pi pair<int,int>
#define pl pair<ll,ll>
#define mii             map<int,int>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf 1e18
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef set<int> si;
const int MOD=1e9+7;
ll lcm(ll a,ll b){return (a * b) / __gcd(a,b);}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll power(ll a,ll b,ll MOD){if(b == 0)return 1;if(b == 1)return a;ll ans = power(a,b/2,MOD) % MOD;ans *= ans;ans %= MOD;if(b % 2 == 1)ans *= a;return ans%MOD;}
ll inverse(ll x){x%=MOD;return power(x,MOD-2,MOD);}
ll fact[1],inv[1];
void factorial(ll n){
  fact[0]=1;
  for(ll i=1;i<=n;i++){
    fact[i]=fact[i-1]*i;
    if(fact[i]>=MOD)
      fact[i]%=MOD;
  }
}
ll ncr(ll n,ll r){
  if(n<r||n<0||r<0)
    return 0;
  ll b=inv[n-r];
  ll c=inv[r];
  ll a=fact[n]*b;
  if(a>=MOD)
    a%=MOD;
  a*=c;
  if(a>=MOD)
    a%=MOD;
  return a;
}
vector<char> is_prime(100001, true);
void sieve(ll n){
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
        if (is_prime[i] && (long long)i * i <= n) {
            for (int j = i * i; j <= n; j += i)
                  is_prime[j] = false;
        }
  }
}

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int t;
    t=1;
    cout<<fixed<<setprecision(0);
     //  cin>>t;
    while(t--){
            ll n,k,ans=0,ans1=0;
            cin>>n>>k;
            string s;cin>>s;s.pb(0);
            if(k==1){s.pop_back();cout<<"0\n"<<s;}
            else if(k==2){
              int a=0,b=0;
              REP(i,0,n-1){
                if(i&1) s[i]!='B'? b++:a++;
                else s[i]!='A'?b++:a++;
              }
              cout<<min(a,b)<<"\n";s.pop_back();
              if(a<b) REP(i,0,n-1){if(i&1)cout<<"B"; else cout<<"A";}
              else REP(i,0,n-1){if(i&1) cout<<"A"; else cout<<"B";}
            }
            else{
              REP(i,1,n-1){
                if(s[i]==s[i-1]){
                 ans++;
                 if(s[i-1]!='A'&&s[i+1]!='A') s[i]='A';
                 else if(s[i-1]!='B'&&s[i+1]!='B') s[i]='B';
                 else if(s[i-1]!='C'&&s[i+1]!='C') s[i]='C';
                }
              }s.pop_back();
              cout<<ans<<"\n"<<s<<"\n";
            }

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

typedef long long ll;
typedef long double ld;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPR(i,a,b) for(int i = b; i >= a; i--)
#define all(a)              a.begin(), a.end()
#define alln(a, n)          (a, a+n)
#define sz(x) (int)(x).size()
#define MAX                 10000007
#define MIN                 -10000007
#define pi pair<int,int>
#define pl pair<ll,ll>
#define mii             map<int,int>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf 1e18
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef set<int> si;
const int MOD=1e9+7;
ll lcm(ll a,ll b){return (a * b) / __gcd(a,b);}
ll gcd(ll a,ll b){return __gcd(a,b);}
ll power(ll a,ll b,ll MOD){if(b == 0)return 1;if(b == 1)return a;ll ans = power(a,b/2,MOD) % MOD;ans *= ans;ans %= MOD;if(b % 2 == 1)ans *= a;return ans%MOD;}
ll inverse(ll x){x%=MOD;return power(x,MOD-2,MOD);}
ll fact[1],inv[1];
void factorial(ll n){
  fact[0]=1;
  for(ll i=1;i<=n;i++){
    fact[i]=fact[i-1]*i;
    if(fact[i]>=MOD)
      fact[i]%=MOD;
  }
}
ll ncr(ll n,ll r){
  if(n<r||n<0||r<0)
    return 0;
  ll b=inv[n-r];
  ll c=inv[r];
  ll a=fact[n]*b;
  if(a>=MOD)
    a%=MOD;
  a*=c;
  if(a>=MOD)
    a%=MOD;
  return a;
}
vector<char> is_prime(100001, true);
void sieve(ll n){
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
        if (is_prime[i] && (long long)i * i <= n) {
            for (int j = i * i; j <= n; j += i)
                  is_prime[j] = false;
        }
  }
}

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int t;
    t=1;
    cout<<fixed<<setprecision(0);
     //  cin>>t;
    while(t--){
            ll n,k,ans=0;
            cin>>n>>k;
            string s;cin>>s;s.pb(0);
            if(k==1){s.pop_back();cout<<"0\n"<<s;}
            else if(k==2){
              REP(i,0,n-2){
                if(s[i]==s[i+1]){
                  ans++;
                  if(s[i]=='A') s[i+1]='B';
                  else s[i+1]='A';
                }
              }s.pop_back();
              cout<<ans<<"\n"<<s;
            }
            else{
              REP(i,1,n-1){
                if(s[i]==s[i-1]){
                 ans++;
                 if(s[i-1]!='A'&&s[i+1]!='A') s[i]='A';
                 else if(s[i-1]!='B'&&s[i+1]!='B') s[i]='B';
                 else if(s[i-1]!='C'&&s[i+1]!='C') s[i]='C';
                }
              }s.pop_back();
              cout<<ans<<"\n"<<s<<"\n";
            }

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
            ll d=0,n,k;
            string s;
            cin>>n>>k>>s;
            REP(i,0,n-2){
              if(s[i]==s[i+1]){
                if(s[i]=='Z') s[i+1]='A';
                else s[i+1]='Z';
                d++;
              }
            }
            cout<<d<<"\n"<<s;

            }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	if (str.size() == 1) {
		cout << 0 << endl << str;
		exit(0);
	}

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			char t = str[i];
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
				}
			}
			else {
				if (str[i] == str[i + 1]) {
					if (t != str[i + 1]) {
						if (str[i] == 'A') {
							str[i - 1] = 'A';
							str[i] = t;
						}
						else {
							str[i - 1] = 'B';
							str[i] = t;
						}
					}
				}
			}
		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 1]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
//	_getch();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	if (str.size() == 1) {
		cout << 0 << endl << str;
		exit(0);
	}

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			char t = str[i];
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
				}
			}
			else {
				if (str[i] == str[i + 1]) {
					if (t != str[i + 1]) {
						if (str[i] == 'A') {
							str[i - 1] = 'A';
							str[i] = t;
						}
						else {
							str[i - 1] = 'B';
							str[i] = t;
						}
					}
				}
			}
		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 2]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	//_getch();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	if (str.size() == 1) {
		cout << 1 << endl << str;
		exit(0);
	}

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
				}
			}
		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 1]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
//	_getch();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	if (str.size() == 1) {
		cout << 0 << endl << str;
		exit(0);
	}

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
				}
			}
		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 1]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	//_getch();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
				}
			}
		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 1]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	//_getch();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	if (str.size() == 1) {
		cout << 0 << endl << str;
		exit(0);
	}

	if (k == 2) {
		int c1 = 0;
		int c2 = 0;
		//string ans1, ans2;
		for (int i = 0; i < n; i++) {
			if (str[i] == 'A' && i % 2 == 0) c1++;
			if (str[i] == 'B' && i % 2 == 1) c1++;
		}

		for (int i = 0; i < n; i++) {
			if (str[i] == 'B' && i % 2 == 0) c2++;
			if (str[i] == 'A' && i % 2 == 1) c2++;
		}

		if (c1 > c2) {
			cout << c2 << endl;
			for (int i = 0; i < n; i++) {
				if (i % 2 == 0) cout << 'A';
				else cout << 'B';
			}
		}
		else {
			cout << c1 << endl;
			for (int i = 0; i < n; i++) {
				if (i % 2 == 0) cout << 'B';
				else cout << 'A';
			}
		}

		exit(0);
	}

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
					if (str[i - 1] == 'C') {
						str[i] = 'B';
					}
				}
			}

		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 2]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	//_getch();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <string.h>
#include <iomanip>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
//#define LOCAL
/*#ifdef LOCAL
fprintf(stderr, "Time: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif*/

using namespace std;

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	ios;
	int n, k;
	cin >> n >> k;
	string str;
	cin >> str;

	vector <char> dp(str.size());
	int count = 0;

	if (str.size() == 1) {
		cout << 0 << endl << str;
		exit(0);
	}

	if (k == 2) {
		int c1 = 0;
		int c2 = 0;
		//string ans1, ans2;
		for (int i = 0; i < n; i++) {
			if (str[i] == 'A' && i % 2 == 0) c1++;
			if (str[i] == 'B' && i % 2 == 1) c1++;
		}

		for (int i = 0; i < n; i++) {
			if (str[i] == 'B' && i % 2 == 0) c2++;
			if (str[i] == 'A' && i % 2 == 1) c2++;
		}

		if (c1 > c2) {
			cout << c2 << endl;
			for (int i = 0; i < n; i++) {
				if (i % 2 == 0) cout << 'A';
				else cout << 'B';
			}
		}
		else {
			cout << c1 << endl;
			for (int i = 0; i < n; i++) {
				if (i % 2 == 0) cout << 'B';
				else cout << 'A';
			}
		}

		exit(0);
	}

	for (int i = 1; i < str.size() - 1; i++) {
		if (str[i] == str[i - 1]) {
			char t = str[i];
			if (str[i - 1] == 'A') str[i] = 'B';
			else str[i] = 'A';
			count++;
			if (k > 2) {
				if (str[i + 1] == str[i]) {
					str[i] = 'C';
				}
			}

		}
	}

	if (str.back() == str[str.size() - 2]) {
		count++;
		str.back() = 'A';
		if (str.back() == str[str.size() - 2]) {
			str.back() = 'B';
		}
	}

	cout << count << endl << str;

#ifdef LOCAL
	fprintf(stderr, "\nTime: %.2lf\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	//_getch();
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <cstdio>
#include <queue>
#include <deque>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

#define x first
#define y second
#define mp make_pair
#define endl '\n'

typedef long long li;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<pii, pii> ppp;
typedef pair<pll, pll> ppl;

const int LIM = 1e5;
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = INF + 7;
const double EPS = 1e-9;

int main() {
	ios_base::sync_with_stdio(false);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n, m;
	string s;
	cin >> n >> m >> s;

		if (n == 3) {
			int min_ = INF;
			string ans = "";

			for (int i = 'A'; i < 'A' + m; i++)
				for (int j = 'A'; j < 'A' + m; j++)
					for (int q = 'A'; q < 'A' + m; q++)
						if (i != j  && j != q){
							int cnt = 0;

							if (i != s[0])
								cnt++;
							if (j != s[1])
								cnt++;
							if (q != s[2])
								cnt++;

							if (cnt < min_) {
								min_ = cnt;
								ans = i;
								ans += j;
								ans += q;
							}
						}

			cout << min_ << endl << ans;
			return 0;
		}

	int ans = 0;

	for (int i = 1; i < n - 1; i++) {
		if (s[i - 1] == s[i] && s[i] == s[i + 1]) {
			s[i] = 'A';

			while (s[i] == s[i + 1])
				s[i]++;

			ans++;
		}
		else
			if (s[i] == s[i - 1]) {
				s[i] = 'A';

				while (int(s[i]) < 'A' + m && (s[i] == s[i + 1] || s[i] == s[i - 1]))
					s[i]++;

				if (s[i] == 'A' + m)
					s[i] = s[i + 1];

				ans++;
			}
	}

	if (s[n - 1] == s[n - 2]) {
		ans++;
		if (s[n - 2] == 'A')
			s[n - 1] = 'B';
		else
			s[n - 1] = 'A';
	}

	cout << ans << endl << s;

	//system("pause");
	return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <cstdio>
#include <queue>
#include <deque>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

#define x first
#define y second
#define mp make_pair
#define endl '\n'

typedef long long li;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<pii, pii> ppp;
typedef pair<pll, pll> ppl;

const int LIM = 1e5;
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = INF + 7;
const double EPS = 1e-9;

int main() {
	ios_base::sync_with_stdio(false);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n, m;
	string s;
	cin >> n >> m >> s;

	int ans = 0;

	for (int i = 1; i < n - 1; i++) {
		if (s[i - 1] == s[i] && s[i] == s[i + 1]) {
			s[i] = 'A';

			while (s[i] == s[i + 1])
				s[i]++;

			ans++;
		}
		else
			if (s[i] == s[i - 1]) {
				s[i] = 'A';

				while (int(s[i]) < 'A' + m && (s[i] == s[i + 1] || s[i] == s[i - 1]))
					s[i]++;

				if (s[i] == 'A' + m)
					s[i] = s[i + 1];

				ans++;
			}
	}

	if (s[n - 1] == s[n - 2]) {
		ans++;
		if (s[n - 2] == 'A')
			s[n - 1] = 'B';
		else
			s[n - 1] = 'A';
	}

	cout << ans << endl << s;

//	system("pause");
	return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <cstdio>
#include <queue>
#include <deque>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

#define x first
#define y second
#define mp make_pair
#define endl '\n'

typedef long long li;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<pii, pii> ppp;
typedef pair<pll, pll> ppl;

const int LIM = 1e5;
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = INF + 7;
const double EPS = 1e-9;

int main() {
	ios_base::sync_with_stdio(false);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n, m;
	string s;
	cin >> n >> m >> s;

		if (n == 3) {
			int min_ = INF;
			string ans = "";

			for (int i = 'A'; i < 'A' + m; i++)
				for (int j = 'A'; j < 'A' + m; j++)
					for (int q = 'A'; q < 'A' + m; q++)
						if (i != j  && j != q){
							int cnt = 0;

							if (i != s[0])
								cnt++;
							if (j != s[0])
								cnt++;
							if (q != s[0])
								cnt++;

							if (cnt < min_) {
								min_ = cnt;
								ans = i;
								ans += j;
								ans += q;
							}
						}

			cout << min_ << endl << ans;
			return 0;
		}

	int ans = 0;

	for (int i = 1; i < n - 1; i++) {
		if (s[i - 1] == s[i] && s[i] == s[i + 1]) {
			s[i] = 'A';

			while (s[i] == s[i + 1])
				s[i]++;

			ans++;
		}
		else
			if (s[i] == s[i - 1]) {
				s[i] = 'A';

				while (int(s[i]) < 'A' + m && (s[i] == s[i + 1] || s[i] == s[i - 1]))
					s[i]++;

				if (s[i] == 'A' + m)
					s[i] = s[i + 1];

				ans++;
			}
	}

	if (s[n - 1] == s[n - 2]) {
		ans++;
		if (s[n - 2] == 'A')
			s[n - 1] = 'B';
		else
			s[n - 1] = 'A';
	}

	cout << ans << endl << s;

	//system("pause");
	return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <cstdio>
#include <queue>
#include <deque>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

#define x first
#define y second
#define mp make_pair
#define endl '\n'

typedef long long li;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<pii, pii> ppp;
typedef pair<pll, pll> ppl;

const int LIM = 1e5;
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = INF + 7;
const double EPS = 1e-9;

int main() {
	ios_base::sync_with_stdio(false);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n, m;
	string s;
	cin >> n >> m >> s;

	if (n==1)
		cout << 0 << endl << s;
	else
		if (n == 3) {
			int min_ = INF;
			string ans = "";

			for (int i = 'A'; i < 'A' + m; i++)
				for (int j = 'A'; j < 'A' + m; j++)
					for (int q = 'A'; q < 'A' + m; q++)
						if (i != j  && j != q){
							int cnt = 0;

							if (i != s[0])
								cnt++;
							if (j != s[0])
								cnt++;
							if (q != s[0])
								cnt++;

							if (cnt < min_) {
								min_ = cnt;
								ans = i;
								ans += j;
								ans += q;
							}
						}

			cout << min_ << endl << ans;
			return 0;
		}

	int ans = 0;

	for (int i = 1; i < n - 1; i++) {
		if (s[i - 1] == s[i] && s[i] == s[i + 1]) {
			s[i] = 'A';

			while (s[i] == s[i + 1])
				s[i]++;

			ans++;
		}
		else
			if (s[i] == s[i - 1]) {
				s[i] = 'A';

				while (int(s[i]) < 'A' + m && (s[i] == s[i + 1] || s[i] == s[i - 1]))
					s[i]++;

				if (s[i] == 'A' + m)
					s[i] = s[i + 1];

				ans++;
			}
	}

	if (s[n - 1] == s[n - 2]) {
		ans++;
		if (s[n - 2] == 'A')
			s[n - 1] = 'B';
		else
			s[n - 1] = 'A';
	}

	cout << ans << endl << s;

	//system("pause");
	return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <cstdio>
#include <queue>
#include <deque>

using namespace std;

//ifstream cin("input.txt");
//ofstream cout("output.txt");

#define x first
#define y second
#define mp make_pair
#define endl '\n'

typedef long long li;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<pii, pii> ppp;
typedef pair<pll, pll> ppl;

const int LIM = 1e5;
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = INF + 7;
const double EPS = 1e-9;

int main() {
	ios_base::sync_with_stdio(false);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n, m;
	string s;
	cin >> n >> m >> s;

	if (m == 2) {
		int cnt1, cnt2;

		cnt1 = cnt2 = 0;

		for (int i = 0; i < n; i++)
			if (i % 2 == 0)
				if (s[i] == 'B')
					cnt1++;
				else
					cnt2++;
			else
				if (s[i] == 'A')
					cnt1++;
				else
					cnt2++;

		if (cnt1 < cnt2) {
			cout << cnt1 << endl;
			for (int i = 0; i < n; i++)
				if (i % 2 == 0)
					cout << 'A';
				else
					cout << 'B';
		}
		else
		{
			cout << cnt2 << endl;
			for (int i = 0; i < n; i++)
				if (i % 2 == 0)
					cout << 'B';
				else
					cout << 'A';
		}
	}
	else {
		vector<int> a(n,1);

		for (int i = 1; i < n; i++)
			if (s[i] == s[i - 1])
				a[i] = a[i - 1] + 1;

		int ans = 0;

		for (int i = n - 1; i >= 0; i--)
			if (a[i] > 1)
				if (a[i] % 2 != 0) {
					ans += a[i] / 2;

					char q;

					if (s[i] == 'A')
						q = 'B';
					else
						q = 'A';

					i--;
					for (; a[i] != 2; i -= 2)
						s[i] = q;
					s[i] = q;
					i--;
				}
				else {
					ans += a[i] / 2;

					char q;

					if (s[i] == 'A')
						q = 'B';
					else
						q = 'A';

					i--;
					for (; a[i] != 1; i -= 2)
						s[i] = q;
					if (i == 0) {
						s[i] = q;
					}
					else {
						s[i] == 'A';
						while (s[i] == s[i - 1] || s[i] == s[i + 1])
							s[i]++;
					}
				}

				cout << ans << endl	<< s;
	}

	//system("pause");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
 #define pp pair<int,int>

 bool compare(pp a, pp b){
         if(a.second == b.second){
             return a.first>b.first;
         }
         else{return a.second>b.second;}
     }
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w",stdout);
    #endif
    int a;
    cin>>a;
    pp p[a];
    for(int i=0;i<a;i++){cin>>p[i].first>>p[i].second;}
    sort(p,p+a,compare);
    int x=1,sum=0;
    for(int i=0;i<a;i++){
        x-=1;
        sum+=p[i].first;
        x+=p[i].second;
        if(x==0) break;
    }
cout<<sum;
     }


#include <bits/stdc++.h>
using namespace std;
 #define pp pair<int,int>

 bool compare(pp a, pp b){
         if(a.second == b.second){
             return a.first>b.second;
         }
         return a.second>b.second;
     }
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w",stdout);
    #endif
    int a;
    cin>>a;
    pp p[a];
    for(int i=0;i<a;i++){cin>>p[i].first>>p[i].second;}
    sort(p,p+a,compare);
    int x=1,sum=0;
    for(int i=0;i<a;i++){cout<<p[i].first<<" "<<p[i].second<<endl;}
    for(int i=0;i<a;i++){
        x-=1;
        sum+=p[i].first;
        x+=p[i].second;
        if(x==0) break;
    }
cout<<sum;
     }


#include <bits/stdc++.h>
using namespace std;
 #define pp pair<int,int>

 bool compare(pp a, pp b){
         if(a.second == b.second){
             return a.first>b.second;
         }
         return a.second>a.first;
     }
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w",stdout);
    #endif
    int a;
    cin>>a;
    pp p[a];
    for(int i=0;i<a;i++){cin>>p[i].first>>p[i].second;}
    sort(p,p+a,compare);
    int x=1,sum=0;
    for(int i=0;i<a;i++){
        x-=1;
        sum+=p[i].first;
        x+=p[i].second;
        if(x==0) break;
    }
cout<<sum;
     }


#include <bits/stdc++.h>
using namespace std;
 #define pp pair<int,int>

 bool compare(pp a, pp b){
         if(a.second == b.second){
             return a.first>b.second;
         }
         return a.second>b.second;
     }
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w",stdout);
    #endif
    int a;
    cin>>a;
    pp p[a];
    for(int i=0;i<a;i++){cin>>p[i].first>>p[i].second;}
    sort(p,p+a,compare);
    int x=1,sum=0;
    for(int i=0;i<a;i++){
        x-=1;
        sum+=p[i].first;
        x+=p[i].second;
        if(x==0) break;
    }
cout<<sum;
     }


#include <iostream>
using namespace std;
int num;
int a[100005],b[100005];
int vist[100005]={0};
int cnt=1;
int so=0;
int maxx=-1;
void dfs(int step)
{
    if(step==0)
      {
        if(so>maxx)
        {maxx=so;}
        return;
      }
    for(int i=0;i<num;i++)
    {
        if(vist[i]==0)
        {
            vist[i]=1;
            cnt--;
            cnt+=b[i];
            so+=a[i];
            dfs(cnt);
            vist[i]=0;
            cnt-=b[i];
            cnt++;
            so-=a[i];
        }
    }
}
int main()
{
    cin>>num;
    for(int i=0;i<num;i++)
        cin>>a[i]>>b[i];
    dfs(cnt);
    cout<<maxx<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int num;
int a[100005],b[100005];
int vist[100005]={0};
int cnt=1;
int so=0;
int maxx=-1;
void dfs(int step,int x)
{
    if(step==0||x==0)
      {
        if(so>maxx)
        {maxx=so;}
        return;
      }
    for(int i=0;i<num;i++)
    {
        if(vist[i]==0)
        {
            vist[i]=1;
            so+=a[i];
            dfs(step-1+b[i],x-1);
            vist[i]=0;
            so-=a[i];
        }
    }
    return;
}
int main()
{
    cin>>num;
    for(int i=0;i<num;i++)
        scanf("%d%d",&a[i],&b[i]);
    dfs(cnt,num);
    cout<<maxx<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int num;
int a[100005],b[100005];
int vist[100005]={0};
int cnt=1;
int so=0;
int maxx=-1;
void dfs(int step,int x)
{
    if(step==0||x==0)
      {
        if(so>maxx)
        {maxx=so;}
        return;
      }
    for(int i=0;i<num;i++)
    {
        if(vist[i]==0)
        {
            vist[i]=1;
            so+=a[i];
            dfs(cnt-1+b[i],x-1);
            vist[i]=0;
            so-=a[i];
        }
    }
    return;
}
int main()
{
    cin>>num;
    for(int i=0;i<num;i++)
        cin>>a[i]>>b[i];
    dfs(cnt,num);
    cout<<maxx<<endl;
    return 0;
}


/*#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int i,n,a,m[1000],m1[1000],t=1,t1=0,k=0;
cin>>n;
for(i=0;i<n;i++)
{
    cin>>m[i]>>m1[i];
    if(m1[i]!=0)
        {
            t+=m1[i]-1;
            t1+=m[i];
            m[i]=0;k++;
        }
}
sort(m,m+n);
reverse(m,m+n);
for(i=0;i<t&&i<n-k;i++)
{t1+=m[i];}
cout<<t1<<endl;
return 0;
}*/
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int a1[1000];
bool cmp(int a,int b)
{return a>b;}
int main()
{
	int n,l=0,a=1,b,c,o=0;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&b,&c);
		if(c){a+=c-1;o+=b;}
		else a1[l++]=b;
	}
	sort(a1,a1+l,cmp);
	for(int i=0;i<l&&i<a;++i)
		o+=a1[i];
	printf("%d\n",o);
}


#include <iostream>
using namespace std;
int num;
int a[100005],b[100005];
int vist[100005]={0};
int cnt=1;
int so=0;
int maxx=-1;
void dfs(int step,int x)
{
    if(step==0||x==0)
      {
        if(so>maxx)
        {maxx=so;}
        return;
      }
    for(int i=0;i<num;i++)
    {
        if(vist[i]==0)
        {
            vist[i]=1;
            cnt--;
            cnt+=b[i];
            so+=a[i];
            x--;
            dfs(cnt,x);
            vist[i]=0;
            cnt-=b[i];
            cnt++;x++;
            so-=a[i];
        }
    }
    return;
}
int main()
{
    cin>>num;
    for(int i=0;i<num;i++)
        cin>>a[i]>>b[i];
    dfs(cnt,num);
    cout<<maxx<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include<algorithm>
using namespace std;
pair <int ,int >a[1010];
int ans=0,k=1,i,n;
int main()
{
    cin>>n;
    for( i=0;i<n;i++)
        cin>>a[i].second>>a[i].first;
    sort(a,a+n);
    for(i=n-1;i>=0&&k>0;i--)
    {
        ans+=a[i].second;
        k+=a[i].first-1;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int num;
int a[100005],b[100005];
int vist[100005]={0};
int cnt=1;
int so=0;
int maxx=-1;
void dfs(int step,int x)
{
    if(step==0||x==0)
      {
        if(so>maxx)
        {maxx=so;}
        return;
      }
    for(int i=0;i<num;i++)
    {
        if(vist[i]==0)
        {
            vist[i]=1;
            so+=a[i];
            dfs(step-1+b[i],x-1);
            vist[i]=0;
            so-=a[i];
        }
    }
    return;
}
int main()
{
    cin>>num;
    for(int i=0;i<num;i++)
        cin>>a[i]>>b[i];
    dfs(cnt,num);
    cout<<maxx<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include<algorithm>
using namespace std;
pair <int ,int >a[1010];
int ans=0,k=1,i,n;
int main()
{
    cin>>n;
    for( i=0;i<n;i++)
        cin>>a[i].second>>a[i].first;
    sort(a,a+n);
    for(i=0;i<n;i++)
        cout<<a[i].second<<" "<<a[i].first<<endl;
    for(i=n-1;i>=0&&k>0;i--)
    {
        ans+=a[i].second;
        k+=a[i].first-1;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#define f first
#define s second
#define mp make_pair

using namespace std;

int n, a, b, k, ans;
pair <int, int> p[1050];

int main () {

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		p[i] = mp(b, a);
	}

	sort(p + 1, p + 1 + n);
	k = 1;
	for (int i = n; i > 0; i--) {
		if (k > 0) {
			k += p[i].f;
			ans += p[i].s;
			k--;
		} else {
			break;
		}
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int n, a[110], b[110];
int k, ans;

int main () {

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
	}

	sort(a + 1, a + 1 + n);
	sort(b + 1, b + 1 + n);
	k = b[n];
	ans += a[n];
	n--;
	while (k && n) {
		ans += a[n];
		k += b[n];
		n--;
		k--;
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int n, a[110], b[110];
int k, ans;

int main () {

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
	}

	sort(a + 1, a + 1 + n);
	sort(b + 1, b + 1 + n);
	k = b[n];
	ans += a[n];
	n--;
	while (k && n) {
		ans += a[n];
		k += b[n];
		n--;
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int n, a[110], b[110];
int k, ans;

int main () {

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
	}

	sort(a + 1, a + 1 + n);
	sort(b + 1, b + 1 + n);
	k = b[n];
	for (int i = n; i >= n - k; i--)
		ans += a[i];

	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>
#define f first
#define s second
#define mp make_pair

using namespace std;

int n, a, b, k, ans;
pair <int, int> p[110];

int main () {

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		p[i] = mp(b, a);
	}

	sort(p + 1, p + 1 + n);
	k = p[n].f;
	ans = p[n].s;
	n--;
	while (k && n) {
		ans += p[n].s;
		k += p[n].f;
		n--;
		k--;
	}

	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>
#define f first
#define s second
#define mp make_pair

using namespace std;

int n, a, b, k, ans;
pair <int, int> p[110];

int main () {

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		p[i] = mp(b, a);
	}

	sort(p + 1, p + 1 + n);
	k = 1;
	for (int i = n; i > 0; i--) {
		if (k > 0) {
			k += p[i].f;
			ans += p[i].s;
			k--;
		} else {
			break;
		}
	}

	cout << ans;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

typedef struct {
ll a;
ll b;
}pt;

bool comp(pt p,pt q){
return p.b>q.a?true:false;
}

 int main(){
 int n,i,j,k,a,b;
 ll sum=0;
 ll test[1005];
 pt p[1005];
ll an[1005],len=0;
 cin>>n;
for(i=0;i<n;i++)   cin>>p[i].a>>p[i].b;
sort(p,p+n,comp);

for(i=0;i<n;i++){
    if(p[i].b==0)
    an[len++]=p[i].a;
}
sort(an,an+len);
if(len==n){
    cout<<an[n-1]<endl;
    return 0;

}
k=1;
i=0;
ll l=0,flag=0;
while(l<n&&k!=0){
    k=p[i].b;
    sum=sum+p[i].a;
    while(k!=0&&l<n){
        sum=sum+an[--len];
        l++;
        k--;
        if(len==0)
        {
            flag=1;
            break;
        }
    }
    i++;
    k=p[i].b;
    if(flag) break;
}
if(!flag)
    cout<<sum<<endl;
else{
    sum=0;
    for(i=0;i<n;i++)
        sum+=p[i].a;

    cout<<sum<<endl;
}

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

typedef struct {
ll a;
ll b;
}pt;

bool comp(pt p,pt q){
return p.b>q.b?true:false;
}

 int main(){
 int n,i,j,k,a,b;
 ll sum=0,sum1=0;
 ll test[1005];
 pt p[1005];
ll an[1005],len=0;
ll flag[1005];
 cin>>n;
for(i=0;i<n;i++)   cin>>p[i].a>>p[i].b;
sort(p,p+n,comp);
for(i=0;i<n-1;i++){
    if(p[i+1].b!=0)
    sum1=sum1+p[i].b-1;
    else
    sum1=sum1+p[i].b;
    if(p[i].b==0)
    an[len++]=p[i].a;
}

if(p[i].b==0)   an[len++]=p[i].a;
sum1=sum1+p[i].b;
for(i=0;i<n;i++) {
    if(p[i].b!=0)
        sum=sum+p[i].a;
}

sort(an,an+len);

ll temp=sum1-len;
//cout<<sum1<<" "<<len<<" ";
i=1;
if(len==n){
    sort(an,an+len);
    cout<<an[n-1]<<endl;
    return 0;

}
if(temp<0){
    while(sum1!=0){
        sum=sum+an[len-i];
        an[len-i]=-1;
        i++;
        sum1--;
    }
}

else
{
    for(i=0;i<n;i++){
        if(p[i].b==0)
            sum=sum+p[i].a;
    }

}

cout<<sum<<endl;

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

typedef struct {
ll a;
ll b;
}pt;

bool comp(pt p,pt q){
return p.b>q.a?true:false;
}

 int main(){
 int n,i,j,k,a,b;
 ll sum=0;
 ll test[1005];
 pt p[1005];
ll an[1005],len=0;
 cin>>n;
for(i=0;i<n;i++)   cin>>p[i].a>>p[i].b;
sort(p,p+n,comp);

for(i=0;i<n;i++){
    if(p[i].b==0)
    an[len++]=p[i].a;
}
sort(an,an+len);
if(len==n){
    cout<<an[n-1]<<endl;
    return 0;

}
k=1;
i=0;
ll l=0,flag=0;
while(l<n&&k!=0){
    k=p[i].b;
    sum=sum+p[i].a;
    while(k!=0&&l<n){
        sum=sum+an[--len];
        l++;
        k--;
        if(len==0)
        {
            flag=1;
            break;
        }
    }
    i++;
    k=p[i].b;
    if(flag) break;
}
if(!flag)
    cout<<sum<<endl;
else{
    sum=0;
    for(i=0;i<n;i++)
        sum+=p[i].a;

    cout<<sum<<endl;
}

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

typedef struct {
ll a;
ll b;
}pt;

bool comp(pt p,pt q){
return p.b>q.a?true:false;
}

 int main(){
 int n,i,j,k,a,b;
 ll sum=0;
 ll test[1005];
 pt p[1005];
ll an[1005],len=0;
 cin>>n;
for(i=0;i<n;i++)   cin>>p[i].a>>p[i].b;
sort(p,p+n,comp);
for(i=0;i<n;i++){
    if(p[i].b==0)
    an[len++]=p[i].a;
}
sort(an,an+len);

k=1;
i=0;
ll l=0,flag=0;
while(l<n&&k!=0){
    k=p[i].b;

    sum=sum+p[i].a;
    while(k!=0&&l<n){
        sum=sum+an[--len];
        l++;
        k--;
        if(len==0)
        {
            flag=1;
            break;
        }
    }
    i++;
    k=p[i].b;
    if(flag) break;
}
if(!flag)
    cout<<sum<<endl;
else{
    sum=0;
    for(i=0;i<n;i++)
        sum+=p[i].a;

    cout<<sum<<endl;
}

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

typedef struct {
ll a;
ll b;
}pt;

bool comp(pt p,pt q){
return p.b>p.a?true:false;
}

 int main(){
 int n,i,j,k,a,b;
 ll sum=0;
 ll test[1005];
 pt p[1005];
 cin>>n;
for(i=0;i<n;i++)   cin>>p[i].a>>p[i].b;

sort(p,p+n,comp);
j=p[0].b;
i=0;
while(j!=0&&i<n){
    k=j;
    while(k!=0&&i<n){
        sum=sum+p[i].a;
        k--;
        j=p[i].b;
        i++;
    }

}
if(i==n)
    cout<<sum<<endl;

else{
        ll len=0;
    for(j=i;j<n;j++){
        test[len++]=p[j].a;
    }
    sort(test,test+len);
     sum=sum+test[len-1];
     cout<<sum<<endl;
}

 }


#include <iostream>
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ll long long int

using namespace std;
using pi = pair<ll, ll>;

bool mycomp(pi a,pi b){
	return a.second>b.second;
}

ll cards(ll n){
	vector<pi> v;
	for(ll i=0;i<n;i++){
		ll points, chances;
		cin>>points>>chances;
		v.pb({points,chances});
	}
	sort(all(v),greater<pi>());
	for(ll i=0;i<n;i++) cout << v[i].first <<" -> "<<v[i].second<<endl;
	ll counter=1,ans=0,i=0;
	while(counter>0){
		counter--;
		ans+=v[i].first;
		counter+=v[i].second;
		i++;
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
	ll n;
	cin>>n;
	ll ans =cards(n);
	cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ll long long int

using namespace std;
using pi = pair<ll, ll>;

bool mycomp(pi a,pi b){
	if(a.second != b.second){
		return a.second > b.second;
	}
	return a.first > b.first;
}

ll cards(ll n){
	vector<pi> v;
	for(ll i=0;i<n;i++){
		ll points,choices;
		cin>>points>>choices;
		v.pb({points,choices});
	}
	sort(all(v), mycomp);
	ll counter=1,ans=0,i=0;
	while(counter>0 && i<n){
		counter--;
		ans+=v[i].first;
		counter+=v[i].second;
		i++;
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
	ll n;
	cin>>n;
	ll ans =cards(n);
	cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ll long long int

using namespace std;
using pi = pair<ll, ll>;

bool mycomp(pi a,pi b){
	return a.second>b.second;
}

ll cards(ll n){
	vector<pi> v;
	for(ll i=0;i<n;i++){
		ll points, chances;
		cin>>points>>chances;
		v.pb({points,chances});
	}
	sort(all(v),greater<pi>());
	// for(ll i=0;i<n;i++) cout << v[i].first <<" -> "<<v[i].second<<endl;
	ll counter=1,ans=0,i=0;
	while(counter>0){
		counter--;
		ans+=v[i].first;
		counter+=v[i].second;
		i++;
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
	ll n;
	cin>>n;
	ll ans =cards(n);
	cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ll long long int

using namespace std;
using pi = pair<ll, ll>;

bool mycomp(pi a,pi b){
	if(a.second != b.second){
		return a.second > b.second;
	}
	return a.first > b.first;
}

ll cards(ll n){
	vector<pi> v;
	for(ll i=0;i<n;i++){
		ll points,choices;
		cin>>points>>choices;
		v.pb({points,choices});
	}
	sort(all(v), mycomp);
	ll counter=1,ans=0,i=0;
	while(counter>0){
		counter--;
		ans+=v[i].first;
		counter+=v[i].second;
		i++;
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
	ll n;
	cin>>n;
	ll ans =cards(n);
	cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define ll long long int

using namespace std;
using pi = pair<ll, ll>;

bool mycomp(pi a,pi b){
	return a.second>=b.second;
}

ll cards(ll n){
	vector<pi> v;
	for(ll i=0;i<n;i++){
		ll points, chances;
		cin>>points>>chances;
		v.pb({points,chances});
	}
	sort(all(v),mycomp);
	ll counter=1,ans=0,i=0;
	while(counter>0){
		counter--;
		ans+=v[i].first;
		counter+=v[i].second;
		i++;
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
	ll n;
	cin>>n;
	ll ans =cards(n);
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool sortbysec(pair<int, int> a, pair<int, int> b){
	if(a.second == b.second)	return (a.first > b.first);
	return (a.second > b.second);
}
int main(){
	int n, a, b;
	cin >> n;
	vector<pair<int, int>> v;
	pair<int, int> p;
	for(int i = 0; i < n; i++){
		cin >> p.first >> p.second;
		v.push_back(p);
	}
	sort(v.begin(), v.end(), sortbysec);
	//sort(v.begin(), v.end(), sortnext);
	// for(int i = 0; i < n; i++){
	// 	cout << v[i].first << " " << v[i].second << endl;
	// 	//v.push_back(p);
	// }
	int counter = 1, sum = 0;
	int i = 0;
	for(i = 0; i < n && counter > 0; i++){
		counter = counter - 1 + v[i].second;
		if(counter <= n)
			sum += v[i].first;
	}
	//if(counter == 0)	sum += v[i].first;
	cout << sum << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool sortbysec(pair<int, int> a, pair<int, int> b){
	if(a.second == b.second)	return (a.first > b.first);
	return (a.second > b.second);
}
int main(){
	int n, a, b;
	cin >> n;
	vector<pair<int, int>> v;
	pair<int, int> p;
	for(int i = 0; i < n; i++){
		cin >> p.first >> p.second;
		v.push_back(p);
	}
	sort(v.begin(), v.end(), sortbysec);
	//sort(v.begin(), v.end(), sortnext);
	// for(int i = 0; i < n; i++){
	// 	cout << v[i].first << " " << v[i].second << endl;
	// 	//v.push_back(p);
	// }
	int counter = 1, sum = 0;
	int i = 0;
	for(i = 0; i < n && counter > 0; i++){
		counter = counter - 1 + v[i].second;
			sum += v[i].first;
	}
	//if(counter == 0)	sum += v[i].first;
	cout << sum << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool sortbysec(pair<int, int> a, pair<int, int> b){
	if(a.second == b.second)	return (a.first > b.first);
	return (a.second > b.second);
}
int main(){
	int n, a, b;
	cin >> n;
	vector<pair<int, int>> v;
	pair<int, int> p;
	for(int i = 0; i < n; i++){
		cin >> p.first >> p.second;
		v.push_back(p);
	}
	sort(v.begin(), v.end(), sortbysec);
	//sort(v.begin(), v.end(), sortnext);
	// for(int i = 0; i < n; i++){
	// 	cout << v[i].first << " " << v[i].second << endl;
	// 	//v.push_back(p);
	// }
	int counter = 1, sum = 0;
	int i = 0;
	for(i = 0; i < n && counter > 0; i++){
		counter = counter - 1 + v[i].second;
		if(counter < n)
			sum += v[i].first;
	}
	//if(counter == 0)	sum += v[i].first;
	cout << sum << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool sortbysec(pair<int, int> a, pair<int, int> b){
	if(a.second == b.second)	return (a.first > b.first);
	return (a.second > b.second);
}
int main(){
	int n, a, b;
	cin >> n;
	vector<pair<int, int>> v;
	pair<int, int> p;
	for(int i = 0; i < n; i++){
		cin >> p.first >> p.second;
		v.push_back(p);
	}
	sort(v.begin(), v.end(), sortbysec);
	//sort(v.begin(), v.end(), sortnext);
	// for(int i = 0; i < n; i++){
	// 	cout << v[i].first << " " << v[i].second << endl;
	// 	//v.push_back(p);
	// }
	int counter = 1, sum = 0;
	int i = 0;
	for(i = 0; i < n && counter > 0; i++){
		counter = counter - 1 + v[i].second;
		sum += v[i].first;
	}
	//if(counter == 0)	sum += v[i].first;
	cout << sum << endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	int p = 0;
	int n2 = 1;
	for(int i = 0; i < n && i <= n2; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	int p = 0;
	int n2 = 1;
	for(int i = 0; n2 < n && n2 > 0; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	long long p = c[0].a;
	int n2 = c[0].b;
	for(int i = 1; n2 < n && n2 > 0; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	long long p = c[0].a;
	long long n2 = c[0].b;

	for(int i = 1; n2 < n && n2 > 0; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	long long p = c[0].a;
	int n2 = c[0].b;

	for(int i = 1; n2 < n && n2 > 0; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	int p = c[0].a;
	int n2 = c[0].b;

	for(int i = 1; n2 < n && n2 > 0; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <climits>
#include <list>
#include <stack>
#include <map>
using namespace std;
#define mod 1000000007;
struct card{
	int a;
	int b;
};
bool func(card & c1,card & c2){
	if(c1.b == c2.b)
		return c1.a > c2.a;
	return c1.b > c2.b;
}
int main(){
	int n;
	cin >> n;
	card c[n];
	for(int i = 0; i < n; i++){
		cin >> c[i].a >> c[i].b;
	}
	sort(c,c+n,func);
	int p = 0;
	int n2 = 1;
	for(int i = 0; i < n || i <= n2; i++){
		p += c[i].a;
		n2--;
		n2 += c[i].b;
	}
	cout<<p;
	return 0;
}