#include #include #include #include #define rep(i,n) for(int i=0; i<(int)(n); i++) using namespace std; using LL = long long; const LL Max_C = 1e8; const int Max_N = 1e5; const LL INF = 1e12; LL c[10][10]; LL dp[Max_N][10]; LL power(LL x, LL n){ LL r = 1; while(n){ if(n & 1) r = r * x; x = x * x; n >>= 1; } return r; } int N, fin; LL dfs(int S, int v){ if(dp[S][v] != INF) return dp[S][v]; int num = power(3, v); if(S / num % 3 == 0) return INF; LL res = INF; rep(i,N){ if(S != fin - 1 and i == v) continue; int prev_num = power(3, i); if(S / prev_num % 3 == 0) continue; int pS = S - num; res = min(res, dfs(pS, i) + c[i][v]); } return dp[S][v] = res; } int main(){ cin >> N; assert(2 <= N and N <= 9); rep(i,N){ rep(j,N){ LL cost; cin >> cost; if(i == j) assert(cost == 0); else assert(1 <= cost and cost <= Max_C); c[i][j] = cost; } } fin = power(3, N); rep(i,fin){ rep(j,N) dp[i][j] = INF; } rep(i,N){ int n = power(3, i); dp[n][i] = 0; } rep(i,N) dfs(fin - 1, i); LL ans = INF; rep(i,N) ans = min(ans, dp[fin - 1][i]); cout << ans << endl; return 0; }