#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define aa array<int,3>
#define fir first
#define sec second
#define pb push_back
const int maxn=(1LL<<17);
int n,bny;
int a[maxn+2],lf[20],rg[20];
bool lvl[20];
void merge(int dep,int l,int r){
if(l==r)return;
int mid=(l+r)/2;
merge(dep+1,l,mid),merge(dep+1,mid+1,r);
vector<int>tmp;
int pos=mid+1;
int inv=0;
for(int q=l;q<=mid;q++){
while(pos<=r && a[q]>a[pos]){
tmp.pb(a[pos]); pos++;
}
tmp.pb(a[q]);
inv+=(pos-mid-1);
}
int leng=(mid-l+1);
lf[dep]+=inv,rg[dep]+=(leng*leng-inv);
for(int q=0;q<tmp.size();q++){
a[l+q]=tmp[q];
}
}
int tmp[maxn+2];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
bny=(1<<n);
for(int q=0;q<bny;q++){
cin>>a[q];
tmp[q]=a[q];
}
if(n==0){
cout<<0<<endl;
return 0;
}
int b[bny+1];
int best=1e18;
vector<int>op;
for(int sh=0;sh<n;sh++){
if(sh>0){
for(int w=0;w<bny;w++){
int hmm=(w>>1);
if((w&1)){
hmm+=(1<<(n-1));
}
b[hmm]=a[w];
}
for(int q=0;q<bny;q++)a[q]=tmp[q]=b[q];
}
for(int q=0;q<n;q++)lvl[q]=false,lf[q]=0,rg[q]=0;
merge(0,0,bny-1);
int total=0;
for(int q=0;q<n;q++){
total+=min(lf[q],rg[q]);
if(lf[q]>rg[q]){
lvl[q]=true;
}
}
if(total<best){
best=total; op.clear();
for(int w=0;w<sh;w++)op.pb(2);
for(int w=0;w<n;w++){
if(lvl[w])op.pb(1);
op.pb(2);
}
}
for(int q=0;q<bny;q++)a[q]=tmp[q];
}
cout<<best<<endl;
for(auto x : op){
cout<<x;
}
cout<<endl;
}