#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class Node
{
public:
    int height, left, right;
    Node(){
        height = left = right = -1;
    }
    bool operator<(const Node& x) const{
        return make_pair(height, make_pair(left, right)) < make_pair(x.height, make_pair(x.left, x.right));
    }
};

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

    int m = (1 << n) + 1;
    vector<Node> node(m);
    vector<set<int> > from(m);

    for(int i=0; i<n-1; ++i){
        for(int j=0; j<(1<<i); ++j){
            int a = (1 << i)       - 1 + j;
            int b = (1 << (i + 1)) - 1 + j * 2;
            node[a].left = b;
            node[a].right = b + 1;
            node[a].height = i;
            from[b].insert(a);
            from[b+1].insert(a);
        }
    }

    string s;
    cin >> s;
    for(int i=0; i<(1<<(n-1)); ++i){
        int a = (1 << (n - 1)) - 1 + i;
        if(s[i*2] == '0'){
            node[a].left = m - 2;
            from[m-2].insert(a);
        }
        else{
            node[a].left = m - 1;
            from[m-1].insert(a);
        }
        if(s[i*2+1] == '0'){
            node[a].right = m - 2;
            from[m-2].insert(a);
        }
        else{
            node[a].right = m - 1;
            from[m-1].insert(a);
        }
    }

    int ret = m - 2;
    vector<bool> check(m, false);
    for(;;){
        bool isChange = false;
        map<Node, int> history;
        for(int x=0; x<m-2; ++x){
            if(check[x])
                continue;

            if(node[x].left == node[x].right){
                int b = node[x].left;
                from[b].erase(x);
                for(set<int>::iterator it=from[x].begin(); it!=from[x].end(); ++it){
                    int a = *it;
                    if(node[a].left == x)
                        node[a].left = b;
                    if(node[a].right == x)
                        node[a].right = b;
                    from[b].insert(a);
                }
                check[x] = true;
                isChange = true;
                break;
            }

            if(history.find(node[x]) != history.end()){
                int b = history[node[x]];
                from[node[b].left].erase(x);
                from[node[b].right].erase(x);
                for(set<int>::iterator it=from[x].begin(); it!=from[x].end(); ++it){
                    int a = *it;
                    if(node[a].left == x)
                        node[a].left = b;
                    if(node[a].right == x)
                        node[a].right = b;
                    from[b].insert(a);
                }
                check[x] = true;
                isChange = true;
                break;
            }

            history[node[x]] = x;
        }

        if(!isChange){
            cout << ret << endl;
            return 0;
        }

        -- ret;
    }
}

Measured memory_bytes: 1400