#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ud unsigned int
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x3f
#define MAX_INF_VAL 0x3f3f3f3f
#define MAX_INF_VAL_LL 0x3f3f3f3f3f3f3f3f
//#define pi 3.141592653589
#define eps 1e-9
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
//#define p 2173412051LL
//#define sz 2

using namespace std;

template< typename T > void read( T &x ) {
	x = 0;
	char ch = getchar();
	ll f = 1;
	while( !isdigit( ch ) ) {
		if( ch == '-' )
		f *= -1;
		ch = getchar();
	}
	while( isdigit( ch ) ) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	x *= f;
}

struct custom_hash {
    static uint64_t splitmix64( uint64_t x ) {
        x += 0x9e3779b97f4a7c15;
        x = ( x ^ ( x >> 30 ) ) * 0xbf58476d1ce4e5b9;
        x = ( x ^ ( x >> 27 ) ) * 0x94d049bb133111eb;
        return x ^ ( x >> 31 );
    }

    size_t operator() ( uint64_t x ) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64( x + FIXED_RANDOM );
    }
};

int sum[ 200010 ][ 26 ];
char s[ 200010 ];

int main() {
	ios::sync_with_stdio( false );
	cin.tie( 0 ), cout.tie( 0 );
	int n;
	cin >> s + 1;
	n = strlen( s + 1 );
	for( int i = 1; i <= n; ++i ) {
		s[ i ] -= 'a';
		for( int j = 0; j < 26; ++j )
			sum[ i ][ j ] = sum[ i - 1 ][ j ];
		++sum[ i ][ s[ i ] ];
	}
	int pre = n + 1;
	ll ans = 0;
	for( int i = n - 1; i; --i ) {
		if( s[ i ] != s[ i + 1 ] )
			continue;
		if( s[ i ] != s[ pre ] ) {
			ans += n - i - 1 - ( sum[ pre - 1 ][ s[ i ] ] - sum[ i + 1 ][ s[ i ] ] );
		} else {
			if( pre > i + 1 )
				ans += pre - i - 2 - ( sum[ pre - 1 ][ s[ i ] ] - sum[ i + 1 ][ s[ i ] ] );
		}
		pre = i;
	}
	cout << ans;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr << '\n'; }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); }

#define        optimize           ios::sync_with_stdio(0); cin.tie(0);
#define PI    acos(-1.0)
#define pb push_back
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define RESET(a, b) memset(a, b, sizeof(a))
#define pii pair <int, int>
#define gcd(a, b) __gcd(a, b)
#define min3(a, b, c) min(c, min(a, b))
#define max3(a, b, c) max(c, max(a, b))

const ll MX = 2e5 + 105;

vector <int> ara;

int main()
{
    optimize
    int n;
    cin>>n;

    vector <int> s;
    for(int i = 0; i < (1<<n); i++){
        int x;
        cin>>x;
        ara.push_back(x);
        s.push_back(x);
    }

    while(s.size() > 1){

        vector <int> val;
        for(auto x: s) val.push_back(x);

//        for(auto x: s) cout<<x<<' ';
//        cout<<endl;

        s.clear();
        for(int i = 0; i < val.size(); i += 2){
            if(val.size() == 2){
                if(val[i] < val[i+1]){
                    s.push_back(val[i]);
                }
                else s.push_back(val[i+1]);
                continue;
            }
            if(val[i] > val[i+1]){
                s.push_back(val[i]);
            }
            else s.push_back(val[i+1]);
        }
    }
//    error(s[0])
    for(int i = 0; i < ara.size(); i++){
        if(ara[i] == s[0]){
            cout<<i+1<<endl;
            break;
        }
    }
}
