#include <bits/stdc++.h>

using namespace std;

const int N = 5e2 + 7;
int n , m; 
vector<pair<int,int> > adj[N * N * 2] ;
int d[N * N * 2] ; 

int id(int x, int y){
       return x * m + y ;
}

void edge(int u ,int v, int c){
       adj[u].push_back({v , c}) ;
}

int dijkstra(int st, int en){
       for(int i = 0 ;i < N * N * 2 ;++ i)d[i] = 1e9 ; 
       d[st] = 0 ; 

       priority_queue<pair<int,int> > pq ; 
       pq.push({0 , st}) ; 

       while(pq.size()){
              int dst = -pq.top().first ; 
              int node = pq.top().second ; 
              pq.pop() ; 

              for(auto u : adj[node]){
                     if(dst + u.second < d[u.first]){
                            d[u.first] = dst + u.second; 
                            pq.push({-d[u.first] , u.first}) ; 
                     }
              }
       }

       return d[en] ; 
}

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
       freopen("in.in", "r", stdin);
#endif
       
       cin >> n >> m; 

       for(int i = 0 ;i < n; ++ i){
              for(int j = 0 ;j + 1<m ; ++ j){
                     int x; cin >> x; 
                     edge( id(i , j) , id(i , j + 1) , x ) ;
                     edge( id(i, j + 1) , id(i , j) , x ) ;        
              }
       }

       for(int i = 0 ; i + 1 < n; ++ i){
              for(int j = 0 ;j < m; ++ j){
                     int x; cin >> x; 
                     edge( id(i , j) , id(i + 1, j) , x ) ;
              }
       }

       for(int i = 0 ;i < n;++ i){
              for(int j = 0 ;j < m ;++ j){
                     edge( id(i , j) , id(i , j) + n * m , 1 ) ; 
                     edge( id(i, j) + n * m, id(i , j) , 0 ) ; 
                     if(i > 0){
                            edge( id(i , j) + n * m, id(i -1, j) + n * m , 1 ) ;
                     }
              }
       }
       cout << dijkstra(0 , n * m - 1) ; 
       return 0;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

int main2() {
  int b = nextLong();
  int N = (1 << b);

  vector<int> A(N);
  REP(i, N) A[i] = nextLong();

  int h = max_element(ALL(A)) - A.begin();
  int ans = -1;
  if (h >= N/2) {
    ans = max_element(A.begin(), A.begin() + N/2) - A.begin();
  } else {
    ans = max_element(A.begin() + N/2, A.begin() + N) - (A.begin());
  }
  cout << (1+ans) << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}