#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<stdio.h>
#include<fstream>
#include<vector>
#include<bitset>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>

using namespace std;

#define all(v)             v.begin(),v.end()
#define sz(v)            ((int)((v).size()))
#define psh(x)                  push_back(x)
#define sc(x)                 scanf("%d",&x)
#define sc2(x,y)         scanf("%d%d",&x,&y)
#define scl(x)              scanf("%lld",&x)
#define lop(i,n)        for(int i=0;i<n;++i)
#define loop(i,f,l)     for(int i=f;i<l;++i)
#define R_(s)         freopen(s, "r", stdin)
#define W_(s)        freopen(s, "w", stdout)
#define R_W R_("input.txt"),W_("output.txt")
#define PI           acos(-1)
#define INF             1<<30
#define DFS_GRAY           -1
#define DFS_WHITE           0
#define DFS_BLACK           1

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<ss, int>     si;
typedef pair<int, ss>     is;
typedef pair<char, int>  chi;
typedef pair<ss, ss>     pss;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<ss>        vs;
typedef vector<ll>       vll;
typedef vector<vll>     vvll;

ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll gcd(ll a, ll b)  { return (b == 0 ? a : gcd(b, a % b)); }
ll sd(ll x)  { return x<10 ? x : x % 10 + sd(x / 10); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }

int main()
{
    ll n, arr[60000],out=0,k=0,target;
    scl(n);
    lop(i, n)scl(arr[i]),k+=arr[i];
    target = k / n;
    lop(i, n)
    {
        if (arr[i] >= target)
        {
            arr[i + 1] += arr[i] - target;
            out += arr[i] - target;
        }
        else
        {
            arr[i + 1] -= target - arr[i];
            out += target - arr[i];
        }
    }
    cout << out;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(v)        ((int)((v).size()))
#define pp(x)               push_back(x)
#define mk(x,y)           make_pair(x,y)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)     for(ll i=0;i<n;i++)
#define loop(i,f,l)  for(ll i=f;i<l;i++)
#define read(s)   freopen(s, "r", stdin)
#define write(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define inf                   1000000000
#define pi             3.141592653589793
#define endl                        "\n"

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef pair<char, int>  chi;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
typedef vector<vii>     vvii;

const double eps = (1e-7);
const int mod = 1000000007;
int dy8[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int dx8[] = { 0, 0, 1, -1, 1, -1, -1, 1 };
#define nearestpoweroftwo(s) ((ll)pw(2.0, (ll)((log((long double)s) / log(2.0)) + 0.5)))
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
int sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
ll sq(ll x){ lop(i, x)if ((ll)i*i > x) return (i - 1); return ll(1); }
//read("input.txt");write("output.txt");
//#include <bits/stdc++.h>
//using namespace std;

int n;
ll arr[50505], sum;
ll a[50505];
int main()
{
    sc(n);
    lop(i, n) scl(arr[i]), sum += arr[i], a[i] = arr[i];
    sum /= n;

    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        if (arr[i]>=sum)
            arr[i + 1] += arr[i] - sum, cnt += arr[i] - sum;
        else
            arr[i + 1] -= sum - arr[i], cnt += sum - arr[i];
    }

    cout << cnt;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <iomanip>
#include <set>
#include <deque>
#include <fstream>

using namespace std;

#define forn(i,n) for (int i = int(0); i < int(n); i++)
#define forb(i,n) for (int i = int(n-1); i >= int(0); i--)
#define for1(i,n) for (int i = int(1); i <= int(n); i++)
#define fore(i,a,n) for (int i = int(a); i < int(n); i++)

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sz size()
#define sqr(x) ((x)*(x))
#define cube(x) ((x)*(x)*(x))
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int base = 1e9;
const int maxn = 1005;
const int MAXN = 5500;
const int logn = 20;
const int inf = 2e9;
const int alpha = 26;
const int my_hash = 29;
const int my_mod = 2495828519;
const long long l_inf = 4e18;
const long long mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-9;

int main() {
	int n, k = 0;
	ll ans = 0;
	scanf("%d", &n);
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		k += a[i];
	}
	k /= n;
	for (int i = 0; i < n - 1; i++) {
		if (a[i] >= k) {
			a[i + 1] += a[i] - k;
			ans += a[i] - k;
			a[i] = k;
			continue;
		}
		int j = i + 1;
		while (a[i] < k) {
			if (a[j] >= k - a[i]) {
				ans += (j - i) * (k - a[i]);
				a[j] -= k - a[i];
				a[i] = k;
			}
			else {
				ans += (j - i) * a[j];
				a[i] += a[j];
				a[j] = 0;
			}
		}
	}
	printf("%I64d", ans);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <iomanip>
#include <set>
#include <deque>
#include <fstream>

using namespace std;

#define forn(i,n) for (int i = int(0); i < int(n); i++)
#define forb(i,n) for (int i = int(n-1); i >= int(0); i--)
#define for1(i,n) for (int i = int(1); i <= int(n); i++)
#define fore(i,a,n) for (int i = int(a); i < int(n); i++)

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sz size()
#define sqr(x) ((x)*(x))
#define cube(x) ((x)*(x)*(x))
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int base = 1e9;
const int maxn = 1005;
const int MAXN = 5500;
const int logn = 20;
const int inf = 2e9;
const int alpha = 26;
const int my_hash = 29;
const int my_mod = 2495828519;
const long long l_inf = 4e18;
const long long mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-9;

int main() {
	int n, k = 0, j = 0;
	ll ans = 0;
	scanf("%d", &n);
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		k += a[i];
	}
	k /= n;
	for (int i = 0; i < n - 1; i++) {
		if (a[i] >= k) {
			a[i + 1] += a[i] - k;
			ans += a[i] - k;
			a[i] = k;
			continue;
		}
		j = max(j, i + 1);
		while (a[i] < k) {
			if (a[j] >= k - a[i]) {
				ans += (j - i) * (k - a[i]);
				a[j] -= k - a[i];
				a[i] = k;
			}
			else {
				ans += (j - i) * a[j];
				a[i] += a[j];
				a[j] = 0;
				j++;
			}

		}
	}
	printf("%I64d", ans);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <iomanip>
#include <set>
#include <deque>
#include <fstream>

using namespace std;

#define forn(i,n) for (int i = int(0); i < int(n); i++)
#define forb(i,n) for (int i = int(n-1); i >= int(0); i--)
#define for1(i,n) for (int i = int(1); i <= int(n); i++)
#define fore(i,a,n) for (int i = int(a); i < int(n); i++)

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sz size()
#define sqr(x) ((x)*(x))
#define cube(x) ((x)*(x)*(x))
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int base = 1e9;
const int maxn = 1005;
const int MAXN = 5500;
const int logn = 20;
const int inf = 2e9;
const int alpha = 26;
const int my_hash = 29;
const int my_mod = 2495828519;
const long long l_inf = 4e18;
const long long mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-9;

int main() {
	ll n, sum = 0, ans = 0;
	scanf("%I64d", &n);
	vector<ll> a(n);
	for (ll i = 0; i < n; i++) {
		scanf("%I64d", &a[i]);
		sum += a[i];
	}
	sum /= n;
	for (ll i = 0; i < n; i++) {
		ans += abs(a[i] - sum);
		if (i < n - 1) a[i + 1] += a[i] - sum;
	}
	printf("%I64d", ans);
}


#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <functional>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <set>
#define int64 long long
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define repd(i,a,b) for (int i = a; i >= b; i--)
#define mp make_pair

using namespace std;

#ifdef _DEBUG
    const int N = 100071;
#else
    const int N = 300000;
#endif

int64 a[N];
int n;

int main()
{
#ifdef _DEBUG

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d", &n);
    rep(i,1,n) scanf("%I64d", &a[i]);
    if (n == 1)
    {
        cout << 0;
        return 0;
    }
    int64 s = 0;
    rep(i,1,n) s += a[i];
    int k = s / n;
    int l = 1, r = 2;
    int64 ans = 0;
    while (true)
    {
        if (l > n) break;
        if (l == r)
        {
            r++;
            continue;
        }
        if (a[l] == k)
        {
            l++;
            continue;
        }
        if (a[r] == k)
        {
            r++;
            continue;
        }
        if (a[l] < k)
        {
            if (a[r] - k >= k - a[l])
            {
                a[r] -= k - a[l];
                ans += int64(r - l) * (k - a[l]);
                l++;
            }
            else if (a[r] >= k)
            {
                a[l] += a[r] - k;
                ans += int64(r - l) * (a[r] - k);
                a[r] = k;
                r++;
            }
            else r++;
        }
        else
        {
            ans += a[l] - k;
            a[l + 1] += a[l] - k;
            l++;
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <functional>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <set>
#define int64 long long
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define repd(i,a,b) for (int i = a; i >= b; i--)
#define mp make_pair

using namespace std;

#ifdef _DEBUG
    const int N = 100071;
#else
    const int N = 300000;
#endif

int a[N], n;

int main()
{
#ifdef _DEBUG

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d", &n);
    rep(i,1,n) scanf("%d", &a[i]);
    if (n == 1)
    {
        cout << 0;
        return 0;
    }
    int64 s = 0;
    rep(i,1,n) s += a[i];
    int k = s / n;
    int l = 1, r = 2;
    int64 ans = 0;
    while (true)
    {
        if (l > n) break;
        if (l == r)
        {
            r++;
            continue;
        }
        if (a[l] == k)
        {
            l++;
            continue;
        }
        if (a[r] == k)
        {
            r++;
            continue;
        }
        if (a[l] < k)
        {
            if (a[r] - k >= k - a[l])
            {
                a[r] -= k - a[l];
                ans += int64(r - l) * (k - a[l]);
                l++;
            }
            else if (a[r] >= k)
            {
                a[l] += a[r] - k;
                ans += int64(r - l) * (a[r] - k);
                a[r] = k;
                r++;
            }
            else r++;
        }
        else
        {
            ans += a[l] - k;
            a[l + 1] += a[l] - k;
            l++;
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <functional>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <set>
#define int64 long long
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define repd(i,a,b) for (int i = a; i >= b; i--)
#define mp make_pair

using namespace std;

#ifdef _DEBUG
    const int N = 100071;
#else
    const int N = 300000;
#endif

int a[N], n;

int main()
{
#ifdef _DEBUG

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d", &n);
    rep(i,1,n) scanf("%d", &a[i]);
    if (n == 1)
    {
        cout << 0;
        return 0;
    }
    int64 s = 0;
    rep(i,1,n) s += a[i];
    int k = s / n;
    int l = 1, r = 2;
    int64 ans = 0;
    while (true)
    {
        if (l > n) break;
        if (a[l] == k)
        {
            l++;
            continue;
        }
        if (a[r] == k)
        {
            r++;
            continue;
        }
        if (a[l] < k)
        {
            if (a[r] - k >= k - a[l])
            {
                a[r] -= k - a[l];
                ans += int64(r - l) * (k - a[l]);
                l++;
            }
            else if (a[r] >= k)
            {
                a[l] += a[r] - k;
                ans += int64(r - l) * (a[r] - k);
                a[r] = k;
                r++;
            }
            else r++;
        }
        else
        {
            ans += a[l] - k;
            a[l + 1] += a[l] - k;
            l++;
        }
    }
    cout << ans;
    return 0;
}


#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <hash_set>
#include <hash_map>
#include <algorithm>

//define NDEBUG
#include <cassert>

#define FAST_READER ios::sync_with_stdio(false); cin.tie(nullptr);
#define all_(v) (v).begin(), (v).end()
#define for_(i, a, b) for (int i = (a); i < (int)(b); i++)
#define ford_(i, a, b) for (int i = (a); i > (int)(b); i--)
#define foreach_(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)
#define sz_(v) ((int)(v).size())
#define mp_ make_pair
#define pb_ push_back
typedef long long LL_;
typedef long double LD_;
template<class T> T sqr_(T a) {return a * a;}
template<class T> T abs_(T a) {return a > 0 ? a : -a;}
template<class T> T sgn_(T a) {return a > 0 ? 1 : (a < 0 ? -1 : 0);}

using namespace std;

int main() {

#ifndef ONLINE_JUDGE
	assert(freopen("input.txt", "r", stdin)); assert(freopen("output.txt", "w", stdout));
#endif

	FAST_READER;

	int n;
	cin >> n;
	vector<long long> v(n);
	for_(i, 0, n) cin >> v[i];
	long long s = 0;
	for_(i, 0, n) s += v[i];
	s /= n;

	long long ret = 0;

	for_(i, 0, n) {
		ret += abs_(v[i] - s);
	}

	cout << ret;

	return 0;
}


#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <hash_set>
#include <hash_map>
#include <algorithm>

//define NDEBUG
#include <cassert>

#define FAST_READER ios::sync_with_stdio(false); cin.tie(nullptr);
#define all_(v) (v).begin(), (v).end()
#define for_(i, a, b) for (int i = (a); i < (int)(b); i++)
#define ford_(i, a, b) for (int i = (a); i > (int)(b); i--)
#define foreach_(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)
#define sz_(v) ((int)(v).size())
#define mp_ make_pair
#define pb_ push_back
typedef long long LL_;
typedef long double LD_;
template<class T> T sqr_(T a) {return a * a;}
template<class T> T abs_(T a) {return a > 0 ? a : -a;}
template<class T> T sgn_(T a) {return a > 0 ? 1 : (a < 0 ? -1 : 0);}

using namespace std;

int main() {

#ifndef ONLINE_JUDGE
	assert(freopen("input.txt", "r", stdin)); assert(freopen("output.txt", "w", stdout));
#endif

	FAST_READER;

	int n;
	cin >> n;
	vector<long long> v(n);
	for_(i, 0, n) cin >> v[i];
	long long s = 0;
	for_(i, 0, n) s += v[i];
	s /= n;

	long long ret = 0;

	for_(i, 0, n - 1) {
		ret += abs_(v[i] - s);
		v[i + 1] += v[i] - s;
	}

	cout << ret;

	return 0;
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <climits>
#include <ctime>

using namespace std;

const int MOD = 1000000007;
//__int64
__int64 a[500000];

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

    __int64 n, m, sum = 0, x = 0, cnt = 0;
    cin >> n;
    bool flg = true;
    for (__int64 i = 1; i <= n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    m = sum / n;
    for (__int64 i = 1; i <= n; ++i) {
        x += a[i] - m;
        cnt += abs(x);
    }
    cout << cnt << endl;

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


#include<iostream>
#include <algorithm>
using namespace std;
int main ()
{int a[50009],s=0,t=0,r=0,z,k;
int n; cin>>n;
for (int i=0; i<n; i++)
{cin>>a[i]; s+=a[i];} s/=n;
for (int i=0; i<n; i++)
{
r=a[i]; z=i+1;
if (a[i]>s) {a[i+1]+=a[i]-s; t+=a[i]-s;} else
if (a[i]<s) do {t+=(z-i)*min(s-r,a[z]); k=r; r+=min(s-r,a[z]); a[z]-=min(s-k,a[z]);   z++;} while (r<s);
}
cout<<t<<endl;
return 0;
}


#include<iostream>
using namespace std;
int main ()
{int a[50009],s=0,t=0,r=0,z;
int n; cin>>n;
for (int i=0; i<n; i++)
{cin>>a[i]; s+=a[i];} s/=n;
for (int i=0; i<n; i++)
{
r=a[i]; z=i+1;
if (a[i]>s) {a[i+1]=a[i]-s; t+=a[i]-s;} else
if (a[i]<s) do {t+=(z-i)*min(s-r,a[z]); a[z]-=min(s-r,a[z]); z++; r+=min(s-r,a[z]);} while (r<s-a[i]);
}
cout<<t<<endl;
return 0;
}


#include<iostream>
using namespace std;
int main ()
{int a[50009],s=0,t=0,r=0,z;
int n; cin>>n;
for (int i=0; i<n; i++)
{cin>>a[i]; s+=a[i];} s/=n;
for (int i=0; i<n; i++)
{
r=a[i]; z=i+1;
if (a[i]>s) {a[i+1]+=a[i]-s; t+=a[i]-s;} else
if (a[i]<s) do {t+=(z-i)*min(s-r,a[z]); a[z]-=min(s-r,a[z]); z++; r+=min(s-r,a[z]);} while (r<s-a[i]);

}
cout<<t<<endl;
return 0;
}


#include<iostream>
#include <algorithm>
using namespace std;
int main ()
{long long int a[50009],t,s;
long long int n; cin>>n;
for (int i=0; i<n; i++)
{cin>>a[i]; s+=a[i];} s/=n;
for (int i=0; i<n; i++)
{t+=abs(a[i]-s);
a[i+1]+=a[i]-s;}
cout<<t<<endl;
return 0;
}


#include<iostream>
using namespace std;
int main ()
{int a[50009],s=0,t=0,r=0,z;
int n; cin>>n;
for (int i=0; i<n; i++)
{cin>>a[i]; s+=a[i];} s/=n;
for (int i=0; i<n; i++)
{
r=a[i]; z=i+1;
if (a[i]>s) {a[i+1]+=a[i]-s; t+=a[i]-s;} else
if (a[i]<s) do {t+=(z-i)*min(s-r,a[z]); a[z]-=min(s-r,a[z]); z++; r+=min(s-r,a[z]);} while (r<s-a[i]);
cout<<t<<endl;
}
cout<<t<<endl;
return 0;
}


#include<iostream>
#include <algorithm>
using namespace std;
int main ()
{long long int a[50009],s=0,t=0,r=0,z,k;
long long int n; cin>>n;
for (int i=0; i<n; i++)
{cin>>a[i]; s+=a[i];} s/=n;
for (int i=0; i<n; i++)
{
r=a[i]; z=i+1;
if (a[i]>s) {a[i+1]+=a[i]-s; t+=a[i]-s;} else
if (a[i]<s) do {t+=(z-i)*min(s-r,a[z]); k=r; r+=min(s-r,a[z]); a[z]-=min(s-k,a[z]);   z++;} while (r<s);
}
cout<<t<<endl;
return 0;
}


#include<iostream>
using namespace std;
int main()
{
	long long int n, i, sum = 0, ave;
	cin >> n;
	long long int *a = new long long int[n];
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	ave = sum / n;
	sum = 0;
	for (i = 0; i < n - 1; i++)
	{
		a[i] -= ave;
		a[i + 1] += a[i];
		sum += abs(a[i]);
	}

	cout << sum;
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int main()
{
	int n ; cin >> n;
	int *a = new int [n];
	long long sum = 0;
	for(int i =0; i<n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	sum = sum / n;

	long long count = 0;
	for(int i=0; i<n-1; i++)
	{
		count = count + abs(a[i] - sum);
		a[i+1] += a[i] - sum;

	}
	cout << count << endl;
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int main()
{
	int n ; cin >> n;
	long long *a = new long long  [n];
	long long sum = 0;
	for(int i =0; i<n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	sum = sum / n;

	long long count = 0;
	for(int i=0; i<n-1; i++)
	{
		count = count + abs(a[i] - sum);
		a[i+1] += a[i] - sum;

	}
	cout << count << endl;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n ; cin >> n;
	int *a = new int [n];
	long long sum = 0;
	for(int i =0; i<n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	sum = sum / n;
	long long count = 0;
	for(int i=0; i<n; i++)
	{
		if (a[i]  > sum )
		{
			count += a[i] - sum ;
		}
	}
	cout << count * 2 << endl;
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int main()
{
	int n ; cin >> n;
	int *a = new int [n];
	long long sum = 0;
	for(int i =0; i<n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	sum = sum / n;
	long long count = 0;
	for(int i=0; i<n; i++)
	{
		count = count + abs(a[i] - sum);
	}
	cout << count << endl;
	return 0;
}


/*
Abolfazl Kazemi

BSc in Software Engineering: IAUN Iran
MSc student of Software Engineering: IUST
UVA, topcoder, codeforces, facebook account: AKJ88
*/

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <string>
#include <sstream>
#include <ctime>
#include <bitset>
#include <cstdlib>

using namespace std;

#define     For(i,a,b)        for (i=a; i<b; i++)
#define     FoR(i,a,b)        for (i=a; i<=b; i++)
#define     Rep(i,a)          for (i=0; i<a; i++)
#define     FIT(it,v)         for (it=(v).begin(); it!=(v).end(); ++it)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     SF                scanf
#define     PF                printf

#define     EPS         1e-8
#define     PI          acos(-1)
#define     MAXN        50005
#define     MOD         1000000007
#define     INF         1000000000
#define     X           first
#define     Y           second

typedef pair<int, int> ii;
typedef pair<double, double> dd;
typedef long long ll;
typedef unsigned long long llu;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<dd> vdd;

int matches[MAXN];

int main(){
    int i, m, n, total, k;
    ll res = 0;
#ifndef ONLINE_JUDGE
    freopen("C:\\acm_inp\\2B.in", "r", stdin);
    //freopen("C:\\acm_inp\\PSEUDO.OUT", "w", stdout);
#endif

    total = 0;
    cin >> n;
    Rep(i, n){
        cin >> matches[i];
        total += matches[i];
    }
    k = total / n;
    For(i, 0, n-1){
        m = matches[i] - k;
        res += abs(m);
        matches[i + 1] += m;
    }
    cout << res;

    return 0;
}


/*
Abolfazl Kazemi

BSc in Software Engineering: IAUN Iran
MSc student of Software Engineering: IUST
UVA, topcoder, codeforces, facebook account: AKJ88
*/

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <string>
#include <sstream>
#include <ctime>
#include <bitset>
#include <cstdlib>

using namespace std;

#define     For(i,a,b)        for (i=a; i<b; i++)
#define     FoR(i,a,b)        for (i=a; i<=b; i++)
#define     Rep(i,a)          for (i=0; i<a; i++)
#define     FIT(it,v)         for (it=(v).begin(); it!=(v).end(); ++it)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     SF                scanf
#define     PF                printf

#define     EPS         1e-8
#define     PI          acos(-1)
#define     MAXN        50005
#define     MOD         1000000007
#define     INF         1000000000
#define     X           first
#define     Y           second

typedef pair<int, int> ii;
typedef pair<double, double> dd;
typedef long long ll;
typedef unsigned long long llu;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<dd> vdd;

ll matches[MAXN];

int main(){
    ll i, m, n, total, k;
    ll res = 0;
#ifndef ONLINE_JUDGE
    freopen("C:\\acm_inp\\2B.in", "r", stdin);
    //freopen("C:\\acm_inp\\PSEUDO.OUT", "w", stdout);
#endif

    total = 0;
    cin >> n;
    Rep(i, n){
        cin >> matches[i];
        total += matches[i];
    }
    k = total / n;
    For(i, 0, n-1){
        m = matches[i] - k;
        res += abs(m);
        matches[i + 1] += m;
    }
    cout << res;

    return 0;
}


/*
Abolfazl Kazemi

BSc in Software Engineering: IAUN Iran
MSc student of Software Engineering: IUST
UVA, topcoder, codeforces, facebook account: AKJ88
*/

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <string>
#include <sstream>
#include <ctime>
#include <bitset>
#include <cstdlib>

using namespace std;

#define     For(i,a,b)        for (i=a; i<b; i++)
#define     FoR(i,a,b)        for (i=a; i<=b; i++)
#define     Rep(i,a)          for (i=0; i<a; i++)
#define     FIT(it,v)         for (it=(v).begin(); it!=(v).end(); ++it)
#define     ALL(v)            (v).begin(),(v).end()
#define     Set(a,x)          memset((a),(x),sizeof(a))
#define     EXIST(a,b)        find(ALL(a),(b))!=(a).end()
#define     Sort(x)           sort(ALL(x))
#define     UNIQUE(v)         Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define     SF                scanf
#define     PF                printf

#define     EPS         1e-8
#define     PI          acos(-1)
#define     MAXN        50005
#define     MOD         1000000007
#define     INF         1000000000
#define     X           first
#define     Y           second

typedef pair<int, int> ii;
typedef pair<double, double> dd;
typedef long long ll;
typedef unsigned long long llu;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<dd> vdd;

int matches[MAXN];

int main(){
    ll i, m, n, total, k;
    ll res = 0;
#ifndef ONLINE_JUDGE
    freopen("C:\\acm_inp\\2B.in", "r", stdin);
    //freopen("C:\\acm_inp\\PSEUDO.OUT", "w", stdout);
#endif

    total = 0;
    cin >> n;
    Rep(i, n){
        cin >> matches[i];
        total += matches[i];
    }
    k = total / n;
    For(i, 0, n-1){
        m = matches[i] - k;
        res += abs(m);
        matches[i + 1] += m;
    }
    cout << res;

    return 0;
}


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <limits.h>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <math.h>
#include <string>
#include <algorithm>

using namespace std;
typedef long long lli;

const int MAX = 50010;

int n;
int a[MAX] = {0};
int i;
lli mid;

int main() {

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

	scanf("%d",&n);
	lli sum = 0;
	for(i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		sum += a[i];
	}

	mid = sum / n;

	lli res = 0;
	lli t = 0;
	for(i = 0; i < n; i++) {
		t = (mid - a[i]) + t;
		res += abs(t);
	}

	printf("%lld",res);

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n,a[50005];
    long long k=0;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;++i)
    {
        scanf("%d", a+i);
        k+=1ll*a[i];
    }
    long long x=k/(1ll*n);
    long long ans=0,p=0;
    for(i=0;i<n;++i)
    {
        p+=x-1ll*a[i];
        ans+=abs(p);
    }
    printf("%lld\n", ans);
    return 0;
}


#include <iostream>
using namespace std;

int n;
long long sum=0;
long long A[50001];
long long sol=0;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>A[i];
		sum+=A[i];
	}
	sum/=n;
	long long cur=0;
	for(int i=0;i<n-1;i++){
		cur+=A[i]-sum;
		sol+=abs(cur);
	}
	cout<<sol<<endl;
}


#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cstdlib>
using namespace std;

#define PII pair<int, int>
#define VI vector<int>
#define MP make_pair<int, int>
#define LL long long
#define MOD 1000000007

int main()
{
#if _MSC_VER == 1500
	freopen("in.txt", "r", stdin);
#endif
	LL a[50005];
	int n;
	scanf("%d", &n);
	LL sum = 0;
	for(int i = 0; i < n; ++ i)
	{
		scanf("%I64d", &a[i]);
		sum += a[i];
	}
	LL def = sum / n;
	LL ans = 0;
	for(int i = 0; i < n; ++ i)
	{
		if(a[i] < def)
			a[i + 1] -= def - a[i], ans += def - a[i];
		else if(a[i] > def)
			a[i + 1] += a[i] - def, ans += a[i] - def;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cstdlib>
using namespace std;

#define PII pair<int, int>
#define VI vector<int>
#define MP make_pair<int, int>
#define LL long long
#define MOD 1000000007

int main()
{
#if _MSC_VER == 1500
	freopen("in.txt", "r", stdin);
#endif
	int a[50005];
	int n;
	scanf("%d", &n);
	LL sum = 0;
	for(int i = 0; i < n; ++ i)
	{
		scanf("%d", &a[i]);
		sum += a[i];
	}
	int def = int(sum / n);
	LL ans = 0;
	for(int i = 0; i < n; ++ i)
	{
		if(a[i] < def)
			a[i + 1] -= def - a[i], ans += def - a[i];
		else if(a[i] > def)
			a[i + 1] += a[i] - def, ans += a[i] - def;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <list>
#include <algorithm>
#include <stack>
#include <functional>
#include <fstream>
#include <deque>
#include <queue>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <climits>

#define N 100003
#define PI 3.1415926535
#define EPS 0.00000001
#define MAX 1e9
#define MIN -(1e9)
#define ll long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fori(i,n) for(int (i) = 0; (i) < n; ++(i))
#define forin(i, n) for(int (i) = n-1; i >= 0; --(i))
#define mp(a, b) make_pair(a, b)
#define MOD 1000000007

using namespace std;

ll a[212345];
vector<pll> big;
vector<pll> small;

int main()
{
    ios_base::sync_with_stdio(0);
    //fpeopen("input.txt", "r", stdin);
    //fpeopen("output.txt", "r", stdout);
    int n, m;
    cin >> n;
    ll sum = 0;
    for(int i = 0; i < n; ++i)
    {
        cin >> a[i];
        sum += a[i];
    }
    sum /= n;
    ll result = 0;
    for(int i = 0; i < n; ++i)
    {
        if(a[i] > sum)
            big.push_back(mp(a[i], i));
        else
            small.push_back(mp(a[i],i));
    }
    int j = 0;
    for(int i = 0; i < small.size(); ++i)
    {
        if(small[i].first != sum)
        {
            while(small[i].first != sum)
            {
                if(big[j].first - sum >= sum - small[i].first)
                {
                    result += abs(big[j].second - small[i].second) * (sum - small[i].first);
                    big[j].first -= sum - small[i].first;
                    small[i].first = sum;
                    if(big[j].first == sum)
                        ++j;
                }
                else
                {
                    result += abs(big[j].second - small[i].second) * (big[j].first - sum);
                    small[i].first += big[j].first - sum;
                    big[j].first = sum;
                    ++j;
                }
            }
        }
    }

    cout << result;
    //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <list>
#include <algorithm>
#include <stack>
#include <functional>
#include <fstream>
#include <deque>
#include <queue>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <climits>

#define N 100003
#define PI 3.1415926535
#define EPS 0.00000001
#define MAX 1e9
#define MIN -(1e9)
#define ll long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fori(i,n) for(int (i) = 0; (i) < n; ++(i))
#define forin(i, n) for(int (i) = n-1; i >= 0; --(i))
#define mp(a, b) make_pair(a, b)
#define MOD 1000000007

using namespace std;

ll a[212345];
vector<pll> v;

int main()
{
    ios_base::sync_with_stdio(0);
    //fpeopen("input.txt", "r", stdin);
    //fpeopen("output.txt", "r", stdout);
    int n, m;
    cin >> n;
    ll sum = 0;
    for(int i = 0; i < n; ++i)
    {
        cin >> a[i];
        sum += a[i];
    }
    sum /= n;
    ll result = 0;
    for(int i = 0; i < n; ++i)
    {
        if(a[i] > sum)
            result += a[i] - sum;
    }

    cout << result;
   // system("pause");
    return 0;
}


#include <stdio.h>
int n;
__int64 a[50005];
int id[50005];
__int64 sum;
int main(){
    scanf("%d",&n);
        for(int i = 0 ; i < n ; i++){
            scanf("%I64d",a+i);
            sum += 1ll*a[i];
        }
        __int64 all = (sum / n);
        a[n] = -1;
        for(int i = n - 1;  i >= 0 ; i --){
            if(a[i] > all) id[i] = i;
            else id[i] = id[i+1];
        }
        int move = 0;
        for(int i = 0 ; i < n ; i ++){
            if(all == a[i]) continue;
            if(all < a[i]){
                 move += a[i] - all;
                 a[i+1] += a[i] - all;
            }
            else{
                int ind = id[i];
                    int remain = all - a[i];
                    move += (ind - i) * remain;
                    a[ind] -= remain;
                    id[ind] = id[ind+1];
            }
        }
        printf("%d",move);
}


#include <stdio.h>
int n;
int a[50005];
int id[50005];
__int64 sum;
int main(){
    scanf("%d",&n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d",a+i);
            sum += 1ll*a[i];
        }
        int all = (sum / n);
        a[n] = -1;
        for(int i = n - 1;  i >= 0 ; i --){
            if(a[i] > all) id[i] = i;
            else id[i] = id[i+1];
        }
        int move = 0;
        for(int i = 0 ; i < n ; i ++){
            if(all == a[i]) continue;
            if(all < a[i]){
                 move += a[i] - all;
                 a[i+1] += a[i] - all;
            }
            else{
                int ind = id[i];
                    int remain = all - a[i];
                    move += (ind - i) * remain;
                    a[ind] -= remain;
                    id[ind] = id[ind+1];
            }
        }
        printf("%d",move);
}


#include <stdio.h>
int n;
int a[50005];
int id[50005];
__int64 sum;
int main(){
    scanf("%d",&n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d",a+i);
            sum += 1ll*a[i];
        }
        int all = sum / (1ll*n);
        id[n] = -1;
        for(int i = n - 1;  i >= 0 ; i --){
            if(a[i] > all) id[i] = i;
            else id[i] = id[i+1];
        }
        __int64 move = 0;
        for(int i = 0 ; i < n ; i ++){
            if(all == a[i]) continue;
            if(all < a[i]){
                 move += 1ll*(a[i] - all);
                 a[i+1] += a[i] - all;
            }
            else{
                int ind = id[i];
                    int remain = all - a[i];
                    move += (1ll*(ind - i) * 1ll*remain);
                    a[ind] -= remain;
                    if(a[ind] <= all)
                    id[ind] = id[ind+1];
            }
        }
        printf("%I64d",move);
}


#include <stdio.h>
int n;
int a[50005];
int id[50005];
__int64 sum;
int main(){
    scanf("%d",&n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d",a+i);
            sum += 1ll*a[i];
        }
        int all = (sum / n);
        a[n] = -1;
        for(int i = n - 1;  i >= 0 ; i --){
            if(a[i] > all) id[i] = i;
            else id[i] = id[i+1];
        }
        __int64 move = 0;
        for(int i = 0 ; i < n ; i ++){
            if(all == a[i]) continue;
            if(all < a[i]){
                 move += 1ll*(a[i] - all);
                 a[i+1] += a[i] - all;
            }
            else{
                int ind = id[i];
                    int remain = all - a[i];
                    move += (1ll*(ind - i) * remain);
                    a[ind] -= remain;
                    id[ind] = id[ind+1];
            }
        }
        printf("%d",move);
}


#include <bits/stdc++.h>

using namespace std;

const int maxN = 500500;

int n,k, ans=0;
char str[maxN];

int main(){

  cin>>n>>k;
  cin>>str;

  if(k==2){
    int ans1=0;
    int ans2=0;
    char str1[maxN], str2[maxN];
    for(int i=0; i<n; i++){
      str1[i] = str[i];
      str2[i] = str[i];
      if(i&1){
        if(str[i] == 'B'){
          ans2++;
          str2[i] = 'A';
        }else{
          ans1++;
          str1[i] = 'B';
        }
      }else{
        if(str[i] == 'B'){
          ans1++;
          str1[i] = 'A';
        }else{
          ans2++;
          str2[i] = 'B';
        }
      }
    }
    cout<<min(ans1, ans2)<<endl;
    if(ans1 < ans2) cout<<str1<<endl;
    else cout<<str2<<endl;
    return 0;
  }
  str[n] = '5';

  int cons=1;
  for(int i=0; i<n; i++){
    if(str[i] != str[i+1]){
      ans += cons>>1;
      if((cons&1) == 0){
        for(int j=0; j<k; j++){
          if('A' + j != str[i-1] && 'A' + j != str[i+1]){
            str[i] = 'A' + j;
            break;
          }
        }
      }
      cons = 0;
    }else{
      if((cons&1) == 0){
        for(int j=0; j<k; j++){
          if('A' + j != str[i-1] && 'A' + j != str[i+1]){
            str[i] = 'A' + j;
            break;
          }
        }
      }
    }
    cons++;
  }
  str[n] = 0;

  cout<<ans<<endl;
  cout<<str<<endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxN = 500500;

int n,k, ans=0;
char str[maxN];

int main(){

  cin>>n>>k;
  cin>>str;

  if(k==2){
    int ans1=0;
    int ans2=0;
    char str1[maxN], str2[maxN];
    for(int i=0; i<n; i++){
      str1[i] = str[i];
      str2[i] = str[i];
      if(i&1){
        if(str[i] == 'B'){
          ans2++;
          str2[i] = 'A';
        }else{
          ans1++;
          str1[i] = 'B';
        }
      }else{
        if(str[i] == 'B'){
          ans1++;
          str1[i] = 'A';
        }else{
          ans2++;
          str2[i] = 'B';
        }
      }
    }
    cout<<min(ans1, ans2)<<endl;
    cout<<ans1<<" "<<ans2<<endl;
    if(ans1 < ans2) cout<<str1<<endl;
    else cout<<str2<<endl;
    return 0;
  }
  str[n] = '5';

  int cons=1;
  for(int i=0; i<n; i++){
    if(str[i] != str[i+1]){
      ans += cons>>1;
      if((cons&1) == 0){
        for(int j=0; j<k; j++){
          if('A' + j != str[i-1] && 'A' + j != str[i+1]){
            str[i] = 'A' + j;
            break;
          }
        }
      }
      cons = 0;
    }else{
      if((cons&1) == 0){
        for(int j=0; j<k; j++){
          if('A' + j != str[i-1] && 'A' + j != str[i+1]){
            str[i] = 'A' + j;
            break;
          }
        }
      }
    }
    cons++;
  }
  str[n] = 0;

  cout<<ans<<endl;
  cout<<str<<endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, k, ans=0;
char str[500100];

int main(){

  cin>>n>>k;
  cin>>(str+1);

  str[0] = '0';
  str[n+1] = '0';

  for(int i=1; i<=n; i++){
    if(str[i] == str[i+1]){
      ans++;
      str[i+1] = '1';
    }
  }

  cout<<ans<<endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, k, ans=0;
char str[500100];

int main(){

  cin>>n>>k;
  cin>>(str+1);

  str[0] = '0';
  str[n+1] = '0';
  str[n+2] = '7';

  for(int i=1; i<=n; i++){
    if(str[i] == str[i+1]){
      ans++;
      str[i+1] = str[i+2];
      for(int j=0; j<k; j++){
        if('A' + j != str[i+2 && 'A' + j != str[i]]){
          str[i+1] = 'A' + j;
          break;
        }
      }
    }
  }

  cout<<ans<<endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long int maxN = 500500;

long int n,k, ans=0;
char str[maxN];
char str1[maxN], str2[maxN];

int main(){

  cin>>n>>k;
  cin>>str;

  if(k==2){
    int ans1=0;
    int ans2=0;
    for(int i=0; i<n; i++){
      str1[i] = str[i];
      str2[i] = str[i];
      if(i&1){
        if(str[i] == 'B'){
          ans2++;
          str2[i] = 'A';
        }else{
          ans1++;
          str1[i] = 'B';
        }
      }else{
        if(str[i] == 'B'){
          ans1++;
          str1[i] = 'A';
        }else{
          ans2++;
          str2[i] = 'B';
        }
      }
    }
    str1[n] = 0;
    str2[n] = 0;
    cout<<min(ans1, ans2)<<endl;
    if(ans1 < ans2){
      cout<<str1<<endl;
    }else{
      cout<<str2<<endl;
    }
    return 0;
  }
  str[n] = '/';

  int cons=1;
  ans = 0;
  for(int i=0; i<n; i++){
    if(str[i] != str[i+1]){
      ans += (cons>>1);
      if((cons&1) == 0){
        for(int j=0; j<k; j++){
          if('A' + j != str[i-1] && 'A' + j != str[i+1]){
            str[i] = 'A' + j;
            break;
          }
        }
      }
      cons = 0;
    }else{
      if((cons&1) == 0){
        for(int j=0; j<k; j++){
          if('A' + j != str[i-1] && 'A' + j != str[i+1]){
            str[i] = 'A' + j;
            break;
          }
        }
      }
    }
    cons++;
  }
  str[n] = 0;

  cout<<ans<<endl;
  cout<<str<<endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif
 	long int n,m;
 	cin>>n>>m;

 	string s;
 	cin>>s;

 	vector<vector<long int>> dp(n,vector<long int>(m,1e18));

 	for(long int i = 0; i < m; i++)
 		dp[0][i] = ((s[0] - 'A') != i);

 	for(long int i = 1; i < n; i++){
 		for(long int j = 0; j < m; j++){
 			for(long int k = 0; k < min(3LL,m); k++){
 				if(j == k)
 					continue;
 				dp[i][j] = min(dp[i][j],dp[i-1][k] + (j != (s[i] - 'A')));
 			}
 		}
  }

   long int minCost = 1e18;
   for(long int i = 0; i < m; i++)
   	minCost = min(minCost,dp[n-1][i]);

   auto copy = minCost;
   long int last = -1;
   string ans;
   for(long int i = n - 1; i >= 0; i--){
   	for(long int j = 0; j < m; j++){
   		if(dp[i][j] == minCost && last != j){
   			ans.push_back('A' + j);
   			minCost -= ((s[i] - 'A') != j);
   			last = j;
   			break;
   		}
   	}
   }
   reverse(ans.begin(),ans.end());
   cout<<copy<<endl<<ans;
   return 0;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif

   long int n,m;
   cin>>n>>m;

   long int ans = 0;

  string s;
  cin>>s;

 	if(m == 2){
 		long int oddAEvenB = 0, oddBEvenA = 0;
 		for(long int i = 0; i < s.size(); i++){
 			if(s[i] == 'A' && i % 2 == 0)
 				oddBEvenA++;
 			else if(s[i] == 'A' && i % 2 == 1)
 				oddAEvenB++;
 			else if(s[i] == 'B' && i % 2 == 0)
 				oddAEvenB++;
 			else
 				oddBEvenA++;
 		}

 		if(oddAEvenB < oddBEvenA){
 			cout<<oddAEvenB<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'A';
 				else
 					cout<<'B';
 		}
 		else{
 			cout<<oddBEvenA<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'B';
 				else
 					cout<<'A';
 		}
 		return 0;
 	}

  for(long int i = 0; i < n; i++){
  	if(i < n - 2 && s[i] == s[i+1] && s[i+1] == s[i+2]){
  		if(s[i] == 'A')
  			s[i] = 'B';
  		else if(s[i] = 'B')
  			s[i] = 'C';
  		else
  			s[i] = 'A';
  		ans++;
  	}
  	else if(i < n - 1 && s[i] == s[i+1]){
  		if(s[i+1] == 'A'){
  			if(i == 0)
  				s[i] = 'B';
  			else if(s[i-1] == 'A')
  				s[i] = 'B';
  			else if(s[i-1] == 'B')
  				s[i] = 'C';
  			else
  				s[i] = 'B';
  		}
  		else if(s[i+1] == 'B'){
  			if(i == 0)
  				s[i] = 'A';
  			else if(s[i-1] == 'A')
  				s[i] = 'C';
  			else if(s[i-1] == 'B')
  				s[i] = 'C';
  			else
  				s[i] = 'A';
  		}
  		else{
  			if(i == 0)
  				s[i] = 'A';
  			else if(s[i-1] == 'A')
  				s[i] = 'B';
  			else if(s[i-1] == 'B')
  				s[i] = 'A';
  			else
  				s[i] = 'A';
  		}
  		ans++;
  	}
  }

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


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif

   long int n,m;
   cin>>n>>m;

   long int ans = 0;

  string s;
  cin>>s;
  for(long int i = 1; i < n; i++){
  	char c = 'A';
  	bool done = false;

  	while(s[i-1] == s[i]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	while(i < n-1 && s[i] == s[i+1]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	while(s[i-1] == s[i]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	while(i < n-1 && s[i] == s[i+1]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	ans += done;
  }

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


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif

   long int n,m;
   cin>>n>>m;

   long int ans = 0;

  string s;
  cin>>s;

 	if(m == 2){
 		long int oddAEvenB = 0, oddBEvenA = 0;
 		for(long int i = 0; i < s.size(); i++){
 			if(s[i] == 'A' && i % 2 == 0)
 				oddBEvenA++;
 			else if(s[i] == 'A' && i % 2 == 1)
 				oddAEvenB++;
 			else if(s[i] == 'B' && i % 2 == 0)
 				oddAEvenB++;
 			else
 				oddBEvenA++;
 		}

 		if(oddAEvenB < oddBEvenA){
 			cout<<oddAEvenB<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'A';
 				else
 					cout<<'B';
 		}
 		else{
 			cout<<oddBEvenA<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'B';
 				else
 					cout<<'A';
 		}
 		return 0;
 	}

  for(long int i = 0; i < n; i++){
  	if(i < n - 2 && s[i] == s[i+1] && s[i+1] == s[i+2]){
  		if(s[i] == 'A')
  			s[i+1] = 'B';
  		else if(s[i] == 'B')
  			s[i+1] = 'C';
  		else
  			s[i+1] = 'A';
  		ans++;
  	}
  	else if(i < n - 1 && s[i] == s[i+1]){
  		if(s[i+1] == 'A'){
  			if(i == 0)
  				s[i] = 'B';
  			else if(s[i-1] == 'A')
  				s[i] = 'B';
  			else if(s[i-1] == 'B')
  				s[i] = 'C';
  			else
  				s[i] = 'B';
  		}
  		else if(s[i+1] == 'B'){
  			if(i == 0)
  				s[i] = 'A';
  			else if(s[i-1] == 'A')
  				s[i] = 'C';
  			else if(s[i-1] == 'B')
  				s[i] = 'C';
  			else
  				s[i] = 'A';
  		}
  		else{
  			if(i == 0)
  				s[i] = 'A';
  			else if(s[i-1] == 'A')
  				s[i] = 'B';
  			else if(s[i-1] == 'B')
  				s[i] = 'A';
  			else
  				s[i] = 'A';
  		}
  		ans++;
  	}
  }

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


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif

   long int n,m;
   cin>>n>>m;

   long int ans = 0;

  string s;
  cin>>s;

 	if(m == 2){
 		long int oddAEvenB = 0, oddBEvenA = 0;
 		for(long int i = 0; i < s.size(); i++){
 			if(s[i] == 'A' && i % 2 == 0)
 				oddBEvenA++;
 			else if(s[i] == 'A' && i % 2 == 1)
 				oddAEvenB++;
 			else if(s[i] == 'B' && i % 2 == 0)
 				oddAEvenB++;
 			else
 				oddBEvenA++;
 		}

 		if(oddAEvenB < oddBEvenA){
 			cout<<oddAEvenB<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'A';
 				else
 					cout<<'B';
 		}
 		else{
 			cout<<oddBEvenA<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'B';
 				else
 					cout<<'A';
 		}
 		return 0;
 	}

  for(long int i = 1; i < n; i++){
  	char c = 'A';
  	bool done = false;

  	while(s[i-1] == s[i]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	while(i < n-1 && s[i] == s[i+1]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	while(s[i-1] == s[i]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	while(i < n-1 && s[i] == s[i+1]){
  		s[i] = c;
  		c++;
  		done = true;
  	}

  	ans += done;
  }

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


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif
 	long int n,m;
 	cin>>n>>m;

 	string s;
 	cin>>s;

 	vector<vector<long int>> dp(n,vector<long int>(m,1e18));

 	for(long int i = 0; i < m; i++)
 		dp[0][i] = ((s[0] - 'A') != i);

 	for(long int i = 1; i < n; i++){
 		for(long int j = 0; j < m; j++){
 			for(long int k = 0; k < m; k++){
 				if(j == k)
 					continue;
 				dp[i][j] = min(dp[i][j],dp[i-1][k] + (j != (s[i] - 'A')));
 			}
 		}
  }

   long int minCost = 1e18;
   for(long int i = 0; i < m; i++)
   	minCost = min(minCost,dp[n-1][i]);

   auto copy = minCost;
   long int last = -1;
   string ans;
   for(long int i = n - 1; i >= 0; i--){
   	for(long int j = 0; j < m; j++){
   		if(dp[i][j] == minCost && last != j){
   			ans.push_back('A' + j);
   			minCost -= ((s[i] - 'A') != j);
   			last = j;
   			break;
   		}
   	}
   }
   reverse(ans.begin(),ans.end());
   cout<<copy<<endl<<ans;
   return 0;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif

   long int n,m;
   cin>>n>>m;

   long int ans = 0;

  string s;
  cin>>s;

 	if(m == 2){
 		long int oddAEvenB = 0, oddBEvenA = 0;
 		for(long int i = 0; i < s.size(); i++){
 			if(s[i] == 'A' && i % 2 == 0)
 				oddBEvenA++;
 			else if(s[i] == 'A' && i % 2 == 1)
 				oddAEvenB++;
 			else if(s[i] == 'B' && i % 2 == 0)
 				oddAEvenB++;
 			else
 				oddBEvenA++;
 		}

 		if(oddAEvenB < oddBEvenA){
 			cout<<oddAEvenB<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'A';
 				else
 					cout<<'B';
 		}
 		else{
 			cout<<oddBEvenA<<endl;
 			for(long int i = 0; i < n; i++)
 				if(i % 2 == 0)
 					cout<<'B';
 				else
 					cout<<'A';
 		}
 		return 0;
 	}

  for(long int i = 0; i < n; i++){
  	if(i < n - 2 && s[i] == s[i+1] && s[i+1] == s[i+2]){
  		if(s[i] == 'A')
  			s[i+1] = 'B';
  		else if(s[i] = 'B')
  			s[i+1] = 'C';
  		else
  			s[i+1] = 'A';
  		ans++;
  	}
  	else if(i < n - 1 && s[i] == s[i+1]){
  		if(s[i+1] == 'A'){
  			if(i == 0)
  				s[i] = 'B';
  			else if(s[i-1] == 'A')
  				s[i] = 'B';
  			else if(s[i-1] == 'B')
  				s[i] = 'C';
  			else
  				s[i] = 'B';
  		}
  		else if(s[i+1] == 'B'){
  			if(i == 0)
  				s[i] = 'A';
  			else if(s[i-1] == 'A')
  				s[i] = 'C';
  			else if(s[i-1] == 'B')
  				s[i] = 'C';
  			else
  				s[i] = 'A';
  		}
  		else{
  			if(i == 0)
  				s[i] = 'A';
  			else if(s[i-1] == 'A')
  				s[i] = 'B';
  			else if(s[i-1] == 'B')
  				s[i] = 'A';
  			else
  				s[i] = 'A';
  		}
  		ans++;
  	}
  }

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


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int main(){
   IOS;
 //    #ifndef ONLINE_JUDGE
 //        freopen("in.txt",  "r",  stdin);
 //        freopen("out.txt", "w", stdout);
 //    #endif
 	long int n,m;
 	cin>>n>>m;

 	string s;
 	cin>>s;

 	vector<vector<long int>> dp(n,vector<long int>(min(m,3LL),1e18));

 	for(long int i = 0; i < m; i++)
 		dp[0][i] = ((s[0] - 'A') != i);

 	for(long int i = 1; i < n; i++){
 		for(long int j = 0; j < m; j++){
 			for(long int k = 0; k < m; k++){
 				if(j == k)
 					continue;
 				dp[i][j] = min(dp[i][j],dp[i-1][k] + (j != (s[i] - 'A')));
 			}
 		}
  }

   long int minCost = 1e18;
   for(long int i = 0; i < m; i++)
   	minCost = min(minCost,dp[n-1][i]);

   auto copy = minCost;
   long int last = -1;
   string ans;
   for(long int i = n - 1; i >= 0; i--){
   	for(long int j = 0; j < m; j++){
   		if(dp[i][j] == minCost && last != j){
   			ans.push_back('A' + j);
   			minCost -= ((s[i] - 'A') != j);
   			last = j;
   			break;
   		}
   	}
   }
   reverse(ans.begin(),ans.end());
   cout<<copy<<endl<<ans;
   return 0;

}


// @author: akp
// Date: 2020-08-27
// Time: 18:51

/*
	There are few things in this code only I can uderstand
	Also I know my code is a lot messy
	The variable names are shitty
	The logic is far far away from simplest logic
	So, it's a warnig : "Don't read this code"
*/

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

#define ll long long
#define ld long double
#define inf (long long)(1e9)
#define M (long long) (1e9 + 7)
#define akp cout<<"akp here\n"<<flush;
#define repi(i,n) for (ll i = 0; i < n; ++i)
#define repia(i,a,n) for (ll i = a; i < n; ++i)
#define repd(i,n) for (ll i = n-1; i >=0; --i)
#define input(a,n) for (ll pqr = 0; pqr < n; ++pqr) cin>>a[pqr];
#define output(a,n) for (ll pqr = 0; pqr < n; ++pqr) cout<<a[pqr]<<" ";cout<<"\n";
#define pb push_back
#define tle2 time(&end); if(double(end - start)>1) return 0;
#define tle1 time_t start,end; time(&start); time(&end);
#define rep(x,y) for(auto x : y)
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define ee double(0.00000000001)
#define pp pair<ll,ll>
istream& operator>>(istream& o, pp& v) {o>>v.ff>>v.ss;return o;}
ostream& operator<<(ostream& o, pp& v) {o<<"("<<v.ff<<","<<v.ss<<")";return o;}
template <typename T>
istream& operator>>(istream& o, vector<T>& v) {repi(i,v.size()) cin>>v[i]; return o;}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {repi(i,v.size()) cout<<v[i]<<" "; return o;}

// #define local_pc
// #warning TODO: REMOVE LOCAL PC define

void solve()
{
	// code here
	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;

	vector<vector<ll> > dp(n+1,vector<ll>(k,INT_MAX));
	repi(i,k)
	dp[0][i] = (i==s[0]-'A' ? 0 : 1 );

	// cout<<dp[0]<<endl;
	repia(i,1,n)
	{
		ll color = s[i]-'A';
		repi(c,k)
		{
			ll cost = (color==c ? 0 : 1 );

			// color previous
			repi(cp,k)
			{
				if(c==cp) continue;
				dp[i][c]=min(dp[i][c],dp[i-1][cp]);
			}
			dp[i][c]+=cost;
		}
	}

	ll ans=INT_MAX;
	ans=*min_element(dp[n-1].begin(),dp[n-1].end());
	cout<<ans<<endl;
}

int main()
{
	//akp was here
	tle1

	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL);
	// cout.tie(NULL);

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	freopen("i","r",stdin);
	freopen("o","w",stdout);
	cout<<"REMOVE local_pc\n\n";
	#endif

	//code here
	solve();

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	cout<<"\nTime taken : "<<(clock()/double(CLOCKS_PER_SEC))*1000<<" ms"<<endl;
	#endif

	return 0;
}


// @author: akp
// Date: 2020-08-27
// Time: 18:51

/*
	There are few things in this code only I can uderstand
	Also I know my code is a lot messy
	The variable names are shitty
	The logic is far far away from simplest logic
	So, it's a warnig : "Don't read this code"
*/

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

#define ll long long
#define ld long double
#define inf (long long)(1e9)
#define M (long long) (1e9 + 7)
#define akp cout<<"akp here\n"<<flush;
#define repi(i,n) for (ll i = 0; i < n; ++i)
#define repia(i,a,n) for (ll i = a; i < n; ++i)
#define repd(i,n) for (ll i = n-1; i >=0; --i)
#define input(a,n) for (ll pqr = 0; pqr < n; ++pqr) cin>>a[pqr];
#define output(a,n) for (ll pqr = 0; pqr < n; ++pqr) cout<<a[pqr]<<" ";cout<<"\n";
#define pb push_back
#define tle2 time(&end); if(double(end - start)>1) return 0;
#define tle1 time_t start,end; time(&start); time(&end);
#define rep(x,y) for(auto x : y)
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define ee double(0.00000000001)
#define pp pair<ll,ll>
istream& operator>>(istream& o, pp& v) {o>>v.ff>>v.ss;return o;}
ostream& operator<<(ostream& o, pp& v) {o<<"("<<v.ff<<","<<v.ss<<")";return o;}
template <typename T>
istream& operator>>(istream& o, vector<T>& v) {repi(i,v.size()) cin>>v[i]; return o;}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {repi(i,v.size()) cout<<v[i]<<" "; return o;}

// #define local_pc
// #warning TODO: REMOVE LOCAL PC define

void solve()
{
	// code here
	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;

	if(k==2)
	{
		ll c1=0,c2=0;

		repi(i,n)
		{
			// ABABABA..
			if(i%2==0 && s[i]!='A') c1++;
			if(i%2!=0 && s[i]!='B') c1++;

			// BABABAB..
			if(i%2==0 && s[i]!='B') c2++;
			if(i%2!=0 && s[i]!='A') c2++;

		}

		if(c1<c2)
		{
			cout<<c1<<endl;
			repi(i,n)
			{
				cout<<(i%2==0 ? "A" : "B");
			}
		}
		else
		{
			cout<<c2<<endl;
			repi(i,n)
			{
				cout<<(i%2==0 ? "B" : "A");
			}
		}
		cout<<endl;

	}
	else
	{
		ll i=0;
		ll ans=0;
		while(i<n)
		{
			ll c=0;
			while(i+c<n && s[i]==s[i+c])
			{
				// cout<<i<<" , ";
				if(c%2==1)
				{
					// cout<<"::";
					char ch = (s[i+c-1]-'A'+1)%26 + 'A';
					// either this is last of string
					// or next charcter is different
					if(i+c==n-1 || ch!=s[i+c+1])
					{
						s[i+c]=ch;
					}
					else
					{
						ch = (s[i+c+1]-'A'+1)%26 + 'A';
						s[i+c] = ch;
					}
				}
				c++;
			}
			i+=c;
			ans+=(c/2);
		}

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

	// vector<vector<ll> > dp(n+1,vector<ll>(k,INT_MAX));
	// repi(i,k)
	// dp[0][i] = (i==s[0]-'A' ? 0 : 1 );

	// // cout<<dp[0]<<endl;
	// repia(i,1,n)
	// {
	// 	ll color = s[i]-'A';
	// 	repi(c,k)
	// 	{
	// 		ll cost = (color==c ? 0 : 1 );

	// 		// color previous
	// 		repi(cp,k)
	// 		{
	// 			if(c==cp) continue;
	// 			dp[i][c]=min(dp[i][c],dp[i-1][cp]);
	// 		}
	// 		dp[i][c]+=cost;
	// 	}
	// }

	// // cout<<dp[n-1]<<"\n";

	// ll len=INT_MAX;
	// len=*min_element(dp[n-1].begin(),dp[n-1].end());
	// cout<<len<<endl;

	// string ans(n,'.');
	// ll idx=0;
	// repi(i,k)
	// {
	// 	if(dp[n-1][i] < dp[n-1][idx])
	// 		idx=i;
	// }
	// ans[n-1]=char(idx+'A');

	// ll pidx=-1,pval=INT_MAX;
	// for(int i=n-2;i>=0;i--)
	// {
	// 	repi(j,k)
	// 	{
	// 		if(j==idx) continue;
	// 		if(dp[i][j]<pval)
	// 		{
	// 			pval=dp[i][j];
	// 			pidx=j;
	// 		}
	// 	}
	// 	ans[i]=char(pidx+'A');
	// 	idx=pidx;
	// 	pval=INT_MAX;
	// }
	// // akp
	// cout<<ans<<endl;
}

int main()
{
	//akp was here
	tle1

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	freopen("i","r",stdin);
	freopen("o","w",stdout);
	cout<<"REMOVE local_pc\n\n";
	#endif

	//code here
	solve();

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	cout<<"\nTime taken : "<<(clock()/double(CLOCKS_PER_SEC))*1000<<" ms"<<endl;
	#endif

	return 0;
}


// @author: akp
// Date: 2020-08-27
// Time: 18:51

/*
	There are few things in this code only I can uderstand
	Also I know my code is a lot messy
	The variable names are shitty
	The logic is far far away from simplest logic
	So, it's a warnig : "Don't read this code"
*/

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

#define ll long long
#define ld long double
#define inf (long long)(1e9)
#define M (long long) (1e9 + 7)
#define akp cout<<"akp here\n"<<flush;
#define repi(i,n) for (ll i = 0; i < n; ++i)
#define repia(i,a,n) for (ll i = a; i < n; ++i)
#define repd(i,n) for (ll i = n-1; i >=0; --i)
#define input(a,n) for (ll pqr = 0; pqr < n; ++pqr) cin>>a[pqr];
#define output(a,n) for (ll pqr = 0; pqr < n; ++pqr) cout<<a[pqr]<<" ";cout<<"\n";
#define pb push_back
#define tle2 time(&end); if(double(end - start)>1) return 0;
#define tle1 time_t start,end; time(&start); time(&end);
#define rep(x,y) for(auto x : y)
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define ee double(0.00000000001)
#define pp pair<ll,ll>
istream& operator>>(istream& o, pp& v) {o>>v.ff>>v.ss;return o;}
ostream& operator<<(ostream& o, pp& v) {o<<"("<<v.ff<<","<<v.ss<<")";return o;}
template <typename T>
istream& operator>>(istream& o, vector<T>& v) {repi(i,v.size()) cin>>v[i]; return o;}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {repi(i,v.size()) cout<<v[i]<<" "; return o;}

// #define local_pc
// #warning TODO: REMOVE LOCAL PC define

void solve()
{
	// code here
	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;

	if(k==2)
	{
		ll c1=0,c2=0;

		repi(i,n)
		{
			// ABABABA..
			if(i%2==0 && s[i]!='A') c1++;
			if(i%2!=0 && s[i]!='B') c1++;

			// BABABAB..
			if(i%2==0 && s[i]!='B') c2++;
			if(i%2!=0 && s[i]!='A') c2++;

		}

		if(c1<c2)
		{
			cout<<c1<<endl;
			repi(i,n)
			{
				cout<<(i%2==0 ? "A" : "B");
			}
		}
		else
		{
			cout<<c2<<endl;
			repi(i,n)
			{
				cout<<(i%2==0 ? "B" : "A");
			}
		}
		cout<<endl;

	}
	else
	{
		ll i=0;
		ll ans=0;
		while(i<n)
		{
			ll c=0;
			while(i+c<n && s[i]==s[i+c])
			{
				// cout<<i<<" , ";
				if(c%2==1)
				{
					// cout<<"::";
					char ch = (s[i+c-1]-'A'+1)%k + 'A';
					// either this is last of string
					// or next charcter is different
					if(i+c==n-1 || ch!=s[i+c+1])
					{
						s[i+c]=ch;
					}
					else
					{
						ch = (s[i+c+1]-'A'+1)%k + 'A';
						s[i+c] = ch;
					}
				}
				c++;
			}
			i+=c;
			ans+=(c/2);
		}

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

	// vector<vector<ll> > dp(n+1,vector<ll>(k,INT_MAX));
	// repi(i,k)
	// dp[0][i] = (i==s[0]-'A' ? 0 : 1 );

	// // cout<<dp[0]<<endl;
	// repia(i,1,n)
	// {
	// 	ll color = s[i]-'A';
	// 	repi(c,k)
	// 	{
	// 		ll cost = (color==c ? 0 : 1 );

	// 		// color previous
	// 		repi(cp,k)
	// 		{
	// 			if(c==cp) continue;
	// 			dp[i][c]=min(dp[i][c],dp[i-1][cp]);
	// 		}
	// 		dp[i][c]+=cost;
	// 	}
	// }

	// // cout<<dp[n-1]<<"\n";

	// ll len=INT_MAX;
	// len=*min_element(dp[n-1].begin(),dp[n-1].end());
	// cout<<len<<endl;

	// string ans(n,'.');
	// ll idx=0;
	// repi(i,k)
	// {
	// 	if(dp[n-1][i] < dp[n-1][idx])
	// 		idx=i;
	// }
	// ans[n-1]=char(idx+'A');

	// ll pidx=-1,pval=INT_MAX;
	// for(int i=n-2;i>=0;i--)
	// {
	// 	repi(j,k)
	// 	{
	// 		if(j==idx) continue;
	// 		if(dp[i][j]<pval)
	// 		{
	// 			pval=dp[i][j];
	// 			pidx=j;
	// 		}
	// 	}
	// 	ans[i]=char(pidx+'A');
	// 	idx=pidx;
	// 	pval=INT_MAX;
	// }
	// // akp
	// cout<<ans<<endl;
}

int main()
{
	//akp was here
	tle1

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	freopen("i","r",stdin);
	freopen("o","w",stdout);
	cout<<"REMOVE local_pc\n\n";
	#endif

	//code here
	solve();

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	cout<<"\nTime taken : "<<(clock()/double(CLOCKS_PER_SEC))*1000<<" ms"<<endl;
	#endif

	return 0;
}


// @author: akp
// Date: 2020-08-27
// Time: 18:51

/*
	There are few things in this code only I can uderstand
	Also I know my code is a lot messy
	The variable names are shitty
	The logic is far far away from simplest logic
	So, it's a warnig : "Don't read this code"
*/

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

#define ll long long
#define ld long double
#define inf (long long)(1e9)
#define M (long long) (1e9 + 7)
#define akp cout<<"akp here\n"<<flush;
#define repi(i,n) for (ll i = 0; i < n; ++i)
#define repia(i,a,n) for (ll i = a; i < n; ++i)
#define repd(i,n) for (ll i = n-1; i >=0; --i)
#define input(a,n) for (ll pqr = 0; pqr < n; ++pqr) cin>>a[pqr];
#define output(a,n) for (ll pqr = 0; pqr < n; ++pqr) cout<<a[pqr]<<" ";cout<<"\n";
#define pb push_back
#define tle2 time(&end); if(double(end - start)>1) return 0;
#define tle1 time_t start,end; time(&start); time(&end);
#define rep(x,y) for(auto x : y)
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define ee double(0.00000000001)
#define pp pair<ll,ll>
istream& operator>>(istream& o, pp& v) {o>>v.ff>>v.ss;return o;}
ostream& operator<<(ostream& o, pp& v) {o<<"("<<v.ff<<","<<v.ss<<")";return o;}
template <typename T>
istream& operator>>(istream& o, vector<T>& v) {repi(i,v.size()) cin>>v[i]; return o;}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {repi(i,v.size()) cout<<v[i]<<" "; return o;}

// #define local_pc
// #warning TODO: REMOVE LOCAL PC define

void solve()
{
	// code here
	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;

	vector<vector<ll> > dp(n+1,vector<ll>(k,INT_MAX));
	repi(i,k)
	dp[0][i] = (i==s[0]-'A' ? 0 : 1 );

	// cout<<dp[0]<<endl;
	repia(i,1,n)
	{
		ll color = s[i]-'A';
		repi(c,k)
		{
			ll cost = (color==c ? 0 : 1 );

			// color previous
			repi(cp,k)
			{
				if(c==cp) continue;
				dp[i][c]=min(dp[i][c],dp[i-1][cp]);
			}
			dp[i][c]+=cost;
		}
	}

	// cout<<dp[n-1]<<"\n";

	ll len=INT_MAX;
	len=*min_element(dp[n-1].begin(),dp[n-1].end());
	cout<<len<<endl;

	string ans(n,'.');
	ll idx=0;
	repi(i,k)
	{
		if(dp[n-1][i] < dp[n-1][idx])
			idx=i;
	}
	ans[n-1]=char(idx+'A');

	ll pidx=-1,pval=INT_MAX;
	for(int i=n-2;i>=0;i--)
	{
		repi(j,k)
		{
			if(j==idx) continue;
			if(dp[i][j]<pval)
			{
				pval=dp[i][j];
				pidx=j;
			}
		}
		ans[i]=char(pidx+'A');
		idx=pidx;
		pval=INT_MAX;
	}
	// akp
	cout<<ans<<endl;
}

int main()
{
	//akp was here
	tle1

	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL);
	// cout.tie(NULL);

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	freopen("i","r",stdin);
	freopen("o","w",stdout);
	cout<<"REMOVE local_pc\n\n";
	#endif

	//code here
	solve();

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	cout<<"\nTime taken : "<<(clock()/double(CLOCKS_PER_SEC))*1000<<" ms"<<endl;
	#endif

	return 0;
}


// @author: akp
// Date: 2020-08-27
// Time: 18:51

/*
	There are few things in this code only I can uderstand
	Also I know my code is a lot messy
	The variable names are shitty
	The logic is far far away from simplest logic
	So, it's a warnig : "Don't read this code"
*/

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

#define ll long long
#define ld long double
#define inf (long long)(1e9)
#define M (long long) (1e9 + 7)
#define akp cout<<"akp here\n"<<flush;
#define repi(i,n) for (ll i = 0; i < n; ++i)
#define repia(i,a,n) for (ll i = a; i < n; ++i)
#define repd(i,n) for (ll i = n-1; i >=0; --i)
#define input(a,n) for (ll pqr = 0; pqr < n; ++pqr) cin>>a[pqr];
#define output(a,n) for (ll pqr = 0; pqr < n; ++pqr) cout<<a[pqr]<<" ";cout<<"\n";
#define pb push_back
#define tle2 time(&end); if(double(end - start)>1) return 0;
#define tle1 time_t start,end; time(&start); time(&end);
#define rep(x,y) for(auto x : y)
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define ee double(0.00000000001)
#define pp pair<ll,ll>
istream& operator>>(istream& o, pp& v) {o>>v.ff>>v.ss;return o;}
ostream& operator<<(ostream& o, pp& v) {o<<"("<<v.ff<<","<<v.ss<<")";return o;}
template <typename T>
istream& operator>>(istream& o, vector<T>& v) {repi(i,v.size()) cin>>v[i]; return o;}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {repi(i,v.size()) cout<<v[i]<<" "; return o;}

// #define local_pc
// #warning TODO: REMOVE LOCAL PC define

void solve()
{
	// code here
	ll n,k;
	cin>>n>>k;
	string s;
	cin>>s;

	vector<vector<ll> > dp(n+1,vector<ll>(k,INT_MAX));
	repi(i,k)
	dp[0][i] = (i==s[0]-'A' ? 0 : 1 );

	// cout<<dp[0]<<endl;
	repia(i,1,n)
	{
		ll color = s[i]-'A';
		repi(c,k)
		{
			ll cost = (color==c ? 0 : 1 );

			// color previous
			repi(cp,k)
			{
				if(c==cp) continue;
				dp[i][c]=min(dp[i][c],dp[i-1][cp]);
			}
			dp[i][c]+=cost;
		}
	}

	// cout<<dp[n-1]<<"\n";

	ll len=INT_MAX;
	len=*min_element(dp[n-1].begin(),dp[n-1].end());
	cout<<len<<endl;

	string ans(n,'.');
	ll idx=0;
	repi(i,k)
	{
		if(dp[n-1][i] < dp[n-1][idx])
			idx=i;
	}
	ans[n-1]=char(idx+'A');

	ll pidx=-1,pval=INT_MAX;
	for(int i=n-2;i>=0;i--)
	{
		repi(j,k)
		{
			if(j==idx) continue;
			if(dp[i][j]<pval)
			{
				pval=dp[i][j];
				pidx=j;
			}
		}
		ans[i]=char(pidx+'A');
		idx=pidx;
		pval=INT_MAX;
	}
	// akp
	cout<<ans<<endl;
}

int main()
{
	//akp was here
	tle1

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	freopen("i","r",stdin);
	freopen("o","w",stdout);
	cout<<"REMOVE local_pc\n\n";
	#endif

	//code here
	solve();

	// #ifndef ONLINE_JUDGE
	#ifdef local_pc
	cout<<"\nTime taken : "<<(clock()/double(CLOCKS_PER_SEC))*1000<<" ms"<<endl;
	#endif

	return 0;
}


/*
code like muthafukah, hack nasa with html :D
*/

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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005
char s[SZ];
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1] || s[i] == s[i+1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    cnt++;
                    break;
                }
            }
        }
    }
    cout << cnt << "\n" << s;
}


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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005
char s[SZ];
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    cnt++;
                    break;
                }
            }
            if (s[i] == s[i-1]){
                for (int j = 0; j < k; ++j)
                {
                    if (j!= s[i-1] - 'A')
                    {
                        char c = j + 'A';
                        s[i-1] = c;
                        cnt++;
                        break;
                    }
                }
            }
        }
    }
    cout << cnt << "\n" << s;
}


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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005
char s[SZ];
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    cnt++;
                    break;
                }
            }
        }
    }
    cout << cnt << "\n" << s;
}


/*
code like muthafukah, hack nasa with html :D
*/

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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005
char s[SZ];
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1] || s[i] == s[i+1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    break;
                }
            }
            cnt++;
        }
    }
    cout << cnt << "\n" << s;
}


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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005

using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
    int cnt1 = 0, cnt2 = 0;
    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    if (k == 2){
        char c1 = 'A', c2 = 'B', ch1 = 'A', ch2 = 'B' ;
        for(int i = 0; i < n; ++i){
            if (ch1 != s[i]) cnt1++;
            if (ch2 != s[i]) cnt2++;
            if (ch1 == 'A'){
                ch1 = 'B';
                ch2 = 'A';
            }
            else {
                ch1 = 'A';
                ch2 = 'B';
            }
        }
        if (cnt1 < cnt2){
            cout << cnt1 << '\n';
            for(int i = 0; i < n; ++i){
                cout << c1;
                if (c1 == 'A') c1 = 'B';
                else c1 = 'A';
            }
        }
        else{
            cout << cnt2 << '\n';
            for(int i = 0; i < n; ++i){
                cout << c2;
                if (c2 == 'A') c2 = 'B';
                else c2 = 'A';
            }
        }
        return 0;
    }

    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    cnt++;
                    break;
                }
            }
        }
    }

    cout << cnt << "\n" << s;
}


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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005

using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
    int cnt1 = 0, cnt2 = 0;
    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    if (k == 2){

        string s1 = "A", s2 = "B";
        for(int i = 1; i < n; ++i){
            if (s1[i-1] == 'A'){
                s1 = s1 + 'B';
                s2 = s2 + 'A';
            }
            else{
                s1 = s1 + 'A';
                s2 = s2 + 'B';
            }
        }
       // cout << s1 << '\n' << s2;
        for(int i = 0; i < n; ++i){
            if(s1[i] != s[i]) {cnt1++;}
            if(s2[i] != s[i]) {cnt2++;}
        }
        if(cnt1 > cnt2){
            cout << cnt2 << "\n" << s2;
            return 0;
        }
        else{
            cout << cnt1 << "\n" << s1;
            return 0;
        }
    }

    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    cnt++;
                    break;
                }
            }
        }
    }

    cout << cnt << "\n" << s;
}


/*
code like muthafukah, hack nasa with html :D
*/

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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005
char s[SZ];
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    for(int i = 1; i < n - 1; ++i)
    {
        if (s[i] == s[i-1] || s[i] == s[i+1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    break;
                }
            }
            cnt++;
        }
    }
    cout << cnt << "\n" << s;
}


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

//#define MXN 1e18
//#define MOD 1000000007
#define SZ 500005

using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
    int n, k, cnt = 0;
    cin >> n >> k;
    cin >> s;
    for(int i = 1; i < n; ++i)
    {
        if (s[i] == s[i-1]){
            for (int j = 0; j < k; ++j)
            {
                if (j!= s[i-1] - 'A' && j!= s[i+1] - 'A')
                {
                    char c = j + 'A';
                    s[i] = c;
                    cnt++;
                    break;
                }
            }
        }
    }
    if (k == 2){
        string s1 = "A", s2 = "B";
        for(int i = 1; i < n; ++i){
            if (s1[i-1] == 'A'){
                s1 = s1 + 'B';
                s2 = s2 + 'A';
            }
            else{
                s1 = s1 + 'A';
                s2 = s2 + 'B';
            }
        }
        int cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < n; ++i){
            if(s1[i] != s[i]) cnt1++;
            if(s2[i] != s[i]) cnt2++;
        }
        if(cnt1 > cnt2){
            cnt = cnt2;
            s = s2;
        }
        else{
            cnt = cnt1;
            s = s1;
        }
    }

    cout << cnt << "\n" << s;
}


#include<bits/stdc++.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define ll long long
using namespace std;
int main()
{
  int n,k;
  cin>>n>>k;
  int arr[n][k];
  string s;
  cin>>s;
  for(int i=0;i<k;i++)
  {
  	if(s[0]-'A'==i)
  	{
  		arr[0][i]=0;
	}
	else
		arr[0][i]=1;
  }
  for(int j=1;j<n;j++)
  {
  	for(int x=0;x<k;x++)
  	{
		int ans=INT_MAX;
	  	for(int i=0;i<k;i++)
		  {
		  	if(x!=i)
		  	{
		  		ans=min(ans,arr[j-1][i]);
			}
		  }
		if(s[j]-'A'==x)
			arr[j][x]=ans;
		else
			arr[j][x]=ans+1;
	}
  }
  int ans=INT_MAX;
  for(int i=0;i<k;i++)
  {
  	ans=min(ans,arr[n-1][i]);
  }
  cout<<ans<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  //  freopen("test.in","r",stdin);

    cin>>n>>k>>s;
    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;

        int p ;
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') && p!=(s[j+1]-'A') && p!=(s[i]-'A')) break;

        for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    return 0 ;
    }


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;

void go(int p , int i , int j)
{

}
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("test.in","r",stdin);

    ll cs;
    cin>>n>>cs>>s;

    if(cs==2)
    {
    string a=s,b=s;
    for(int i=0;i<s.length();i+=2)
        a[i]='A',b[i]='B';
    for(int i=1;i<s.length();i+=2)
        a[i]='B',b[i]='A';

    ll ans1 = 0,ans2 = 0;
    for(int i=0;i<s.length();i++)
        if(s[i]==a[i]) ans1++;
        else ans2++;

    if(ans1<ans2)
        {
            cout<<ans1<<'\n'<<b<<'\n';
            return 0;

        }
    cout<<ans2<<'\n'<<a<<'\n';
    return 0;
    }

    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;
        if(i==j) continue ;
        int len = j - i + 1 ;
        int p ;
        if(len%2)
        {
        for(p=0;p<26;p++)
            if(p!=(s[i]-'A')) break;
        for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

        continue ;
        }
        for(p=0;p<26;p++)
            if(p!=(s[j+1]-'A') && p!=(s[i]-'A')) break;

        if(p<cs)
         {
         for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

         }
        else
        {
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') &&  p!=(s[i]-'A')) break;

        for(int k = i;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;
        }

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    return 0 ;
    }


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  //  freopen("test.in","r",stdin);

    cin>>n>>k>>s;
    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;

        int p ;
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') &&  p!=(s[i]-'A')) break;

        for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    return 0 ;
    }


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;

void go(int p , int i , int j)
{

}
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("test.in","r",stdin);

    cin>>n>>k>>s;
    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;

        int p ;
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') &&  p!=(s[i]-'A')) break;
        if(p<k)
         {
         for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

         }
        else
        {
        for(p=0;p<26;p++)
            if(p!=(s[j+1]-'A') &&  p!=(s[i]-'A')) break;

        for(int k = i;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;
        }

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    return 0 ;
    }


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;

void go(int p , int i , int j)
{

}
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  //  freopen("test.in","r",stdin);

    cin>>n>>k>>s;
    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;
        if(i==j) continue ;

        int p ;
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') &&  p!=(s[i]-'A')) break;
        if(p<k)
         {
         for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

         }
        else
        {
        for(p=0;p<26;p++)
            if(p!=(s[j+1]-'A') &&  p!=(s[i]-'A')) break;

        for(int k = i;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;
        }

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    return 0 ;
    }


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;

void go(int p , int i , int j)
{

}
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("test.in","r",stdin);

    cin>>n>>k>>s;
    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;
        if(i==j) continue ;
        int len = j - i + 1 ;
        int p ;
        if(len%2)
        {
        for(p=0;p<26;p++)
            if(p!=(s[i]-'A')) break;
        for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

        continue ;
        }
        for(p=0;p<26;p++)
            if(p!=(s[j+1]-'A') && p!=(s[i]-'A')) break;

        if(p<k)
         {
         for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

         }
        else
        {
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') &&  p!=(s[i]-'A')) break;

        for(int k = i;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;
        }

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    return 0 ;
    }


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll ;
const ll MOD=1e9 + 7 ;
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
const ll dx[4] = { -1,1,0,0 };
const ll dy[4] = { 0,0,-1,1 };
const ll MAX = 1e3 +  50 ;
ll n,x,y,k,m,ans,a,b;string s ; bool ok ;

void go(int p , int i , int j)
{

}
signed main()
{

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
   // freopen("test.in","r",stdin);

    cin>>n>>k>>s;
    s+="0";
    s.insert(s.begin(),'0');
    ans = 0 ;
    for(int i = 1 ;i<s.length();i++)
    {
        int j = i+1;
        while(s[j]==s[i]) j++;
        j--;
        if(i==j) continue ;
        int len = j - i + 1 ;
        int p ;
        if(len%2)
        {
        for(p=0;p<26;p++)
            if(p!=(s[i]-'A')) break;
        for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

        continue ;
        }
        for(p=0;p<26;p++)
            if(p!=(s[i]-'A')) break;

        if(p<k)
         {
         for(int k = i+1;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;

         }
        else
        {
        for(p=0;p<26;p++)
            if(p!=(s[i-1]-'A') &&  p!=(s[i]-'A')) break;

        for(int k = i;k<=j;k+=2)
            s[k] = (char)(p+'A'),ans++;
        }

        i = j ;

    }
    s.erase(s.begin());
    s.erase(s.begin()+s.length()-1);
    cout<<ans<<'\n'<<s<<'\n';
    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(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++)
		{
			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]=='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;
}