#include <bits/stdc++.h>

using namespace std;

const int maxn = 80;
const int maxk = 10;
const int inf = 0x3f3f3f3f;

int n, k;
int w[maxn][maxn], col[maxn];

struct mat {
	int a[maxn][maxn];
} st, e;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(seed);

mat operator*(const mat &m1, const mat &m2) {
	mat ret;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			ret.a[i][j] = inf;
			for (int k = 0; k < n; k++) {
				ret.a[i][j] = min(ret.a[i][j], m1.a[i][k] + m2.a[k][j]);
			}
		}
	}
	return ret;
}

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &w[i][j]);
		}
	}
	int T = 50, ans = inf;
	while (T --) {
		for (int i = 0; i < n; i++) col[i] = rand_num() % 2;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (col[i] == col[j]) e.a[i][j] = inf;
				else e.a[i][j] = w[i][j];
				st.a[i][j] = (i == j) ? 0 : inf;
			}
		}
		for (int i = 0; i < k; i++) st = st * e;
		for (int i = 0; i < n; i++) ans = min(ans, st.a[i][i]);
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define rei register int
using namespace std;
const int maxn = 81,T = 20000;
int n,step,ans = 1000000000,id[maxn];
int edge[maxn][maxn],dp[2][maxn];
int main(){
	scanf("%d%d",&n,&step); for(rei i = 1;i <= n;++i) for(rei j = 1;j <= n;++j) scanf("%d",&edge[i][j]);
	for(rei i = 1;i <= T;++i){ id[1] = 0; for(rei j = 2;j <= n;++j) id[j] = rand() % 2;
//		for(rei j = 1;j <= n;++j) cerr << id[j] << " "; cerr << endl;
		for(rei j = 1;j <= n;++j) dp[0][j] = dp[1][j] = 1000000000; dp[1][1] = 0;
		for(rei j = 1,ix = 1;j <= step;++j,ix ^= 1){
		for(rei k = 1;k <= n;++k) dp[ix ^ 1][k] = 1000000000;
		 for(rei k = 1;k <= n;++k)
		  for(rei to = 1;to <= n;++to)
		  if(id[to] == ix) dp[ix ^ 1][to] = min(dp[ix ^ 1][to],dp[ix][k] + edge[k][to]);
//		 for(rei k = 1;k <= n;++k) cerr << dp[ix ^ 1][k] << " "; cerr << endl;
	    } ans = min(ans,dp[1][1]);
	} cout << ans; return 0;
}


#include <bits/stdc++.h>
#define rei register int
using namespace std;
const int maxn = 81,T = 20000;
int n,step,ans = 100000000,id[maxn];
int edge[maxn][maxn],dp[2][maxn];
int main(){
	scanf("%d%d",&n,&step); for(rei i = 1;i <= n;++i) for(rei j = 1;j <= n;++j) scanf("%d",&edge[i][j]);
	for(rei i = 1;i <= T;++i){ id[1] = 0; for(rei j = 2;j <= n;++j) id[j] = rand() % 2;
//		for(rei j = 1;j <= n;++j) cerr << id[j] << " "; cerr << endl;
		for(rei j = 1;j <= n;++j) dp[0][j] = dp[1][j] = 100000000; dp[1][1] = 0;
		for(rei j = 1,ix = 1;j <= step;++j,ix ^= 1){
		for(rei k = 1;k <= n;++k) dp[ix ^ 1][k] = 100000000;
		 for(rei k = 1;k <= n;++k)
		  for(rei to = 1;to <= n;++to)
		  if(id[to] == ix) dp[ix ^ 1][to] = min(dp[ix ^ 1][to],dp[ix][k] + edge[k][to]);
//		 for(rei k = 1;k <= n;++k) cerr << dp[ix ^ 1][k] << " "; cerr << endl;
	    } ans = min(ans,dp[1][1]);
	} cout << ans; return 0;
}


#include "bits/stdc++.h"

using namespace std;

bool cmp(const pair<int,int>& lhs, const pair<int,int>& rhs) {
    return lhs.second < rhs.second;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    int g[100][100];
    vector<pair<int,int>> turnCost[100][100];
    cin>>n>>k;
    for (int i=0;i<n;i++) {
        for (int j=0;j<n;j++) {
            cin>>g[i][j];
        }
    }
    for (int i=0;i<n;i++) {
        for (int j=0;j<n;j++) {
            for (int k=0;k<n;k++) {
                if (k == i || k == j) continue;
                turnCost[i][j].push_back(make_pair(k,g[i][k]+g[k][j]));
            }
            sort(turnCost[i][j].begin(),turnCost[i][j].end(),cmp);
        }
    }
    int ans = 2147483647;
    if (k == 2) {
        cout<<turnCost[0][0][0].second<<endl;
        return 0;
    }
    for (int a=0;a<n;a++) {
        if (k == 4) {
            int tmp = 0;
            auto it = turnCost[0][a].begin();
            while (it != turnCost[0][a].end() && (it->first == a || it->first == 0)) it++;
            if (it == turnCost[0][a].end()) continue;
            tmp += it->second;
            it = turnCost[a][0].begin();
            while (it != turnCost[a][0].end() && (it->first == a || it->first == 0)) it++;
            if (it == turnCost[a][0].end()) continue;
            tmp += it->second;
            ans = min(ans,tmp);
            continue;
        }
        for (int b=0;b<n;b++) {
            if (k == 6) {
                int tmp = 0;
                auto it = turnCost[0][a].begin();
                while (it != turnCost[0][a].end() && (it->first == a || it->first == 0 || it->first == b)) it++;
                if (it == turnCost[0][a].end()) continue;
                tmp += it->second;
                it = turnCost[a][b].begin();
                while (it != turnCost[a][b].end() && (it->first == a || it->first == 0 || it->first == b)) it++;
                if (it == turnCost[a][b].end()) continue;
                tmp += it->second;
                it = turnCost[b][0].begin();
                while (it != turnCost[b][0].end() && (it->first == a || it->first == 0 || it->first == b)) it++;
                if (it == turnCost[b][0].end()) continue;
                tmp += it->second;
                ans = min(ans,tmp);
                continue;
            }
            for (int c=0;c<n;c++) {
                if (k == 8) {
                    int tmp = 0;
                    auto it = turnCost[0][a].begin();
                    while (it != turnCost[0][a].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c)) it++;
                    if (it == turnCost[0][a].end()) continue;
                    tmp += it->second;
                    it = turnCost[a][b].begin();
                    while (it != turnCost[a][b].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c)) it++;
                    if (it == turnCost[a][b].end()) continue;
                    tmp += it->second;
                    it = turnCost[b][c].begin();
                    while (it != turnCost[b][c].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c)) it++;
                    if (it == turnCost[b][c].end()) continue;
                    tmp += it->second;
                    it = turnCost[c][0].begin();
                    while (it != turnCost[c][0].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c)) it++;
                    if (it == turnCost[c][0].end()) continue;
                    tmp += it->second;
                    ans = min(ans,tmp);
                    continue;
                }
                for (int d=0;d<n;d++) {
                    int tmp = 0;
                    auto it = turnCost[0][a].begin();
                    while (it != turnCost[0][a].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c || it->first == d)) it++;
                    if (it == turnCost[0][a].end()) continue;
                    tmp += it->second;
                    it = turnCost[a][b].begin();
                    while (it != turnCost[a][b].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c || it->first == d)) it++;
                    if (it == turnCost[a][b].end()) continue;
                    tmp += it->second;
                    it = turnCost[b][c].begin();
                    while (it != turnCost[b][c].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c || it->first == d)) it++;
                    if (it == turnCost[b][c].end()) continue;
                    tmp += it->second;
                    it = turnCost[c][d].begin();
                    while (it != turnCost[c][d].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c || it->first == d)) it++;
                    if (it == turnCost[c][d].end()) continue;
                    tmp += it->second;
                    it = turnCost[d][0].begin();
                    while (it != turnCost[d][0].end() && (it->first == a || it->first == 0 || it->first == b || it->first == c || it->first == d)) it++;
                    if (it == turnCost[d][0].end()) continue;
                    tmp += it->second;
                    ans = min(ans,tmp);
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
//	FOR(i,1,m-1){
//		FOR(j,1,n)a[j][j]=INF;
//		mul();
//	}
//	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
//	cout<<ans<<'\n';
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
	FOR(i,1,tot)tag[t[i]]=0;
//	if(tmp[1][1]==0){
//		cout<<tot<<'\n';
//		FOR(i,1,tot)cout<<t[i]<<' ';
//		exit(0);
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1,ans=0x7fffffff;
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	if(m>=5&&n>m){
		tot=5;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)FOR(p,k+1,n)
			t[2]=i,t[3]=j,t[4]=k,t[5]=p,ans=min(ans,solve());

	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=0x3f3f3f3f,M=7;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][M],d[N][N][M];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,M-1)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
//	FOR(i,1,m-1){
//		FOR(j,1,n)a[j][j]=INF;
//		mul();
//	}
//	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
//	cout<<ans<<'\n';
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,M-1)if(!tag[d[x][y][i]])
		return c[x][y][i];
	exit(1);
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
	FOR(i,1,tot)tag[t[i]]=0;
//	if(tmp[1][1]==0){
//		cout<<tot<<'\n';
//		FOR(i,1,tot)cout<<t[i]<<' ';
//		exit(0);
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1,ans=0x7fffffff;
	if(m>=1&&n>m){
		tot=1,ans=solve();
	}
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	if(m>=5&&n>m){
		tot=5;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)FOR(p,k+1,n)
			t[2]=i,t[3]=j,t[4]=k,t[5]=p,ans=min(ans,solve());

	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			FOR(j,i+1,5)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		ins(i,j,a[i][k]+a[k][j],k);
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
//	if(tmp[1][1]==0){
//		tmp[1][1]=0;
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2){
		tot=2;
		FOR(i,2,n)t[2]=i,ans=min(ans,solve());
	}
	if(m>=3){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			FOR(j,i+1,5)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		ins(i,j,a[i][k]+a[k][j],k);
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
//	if(tmp[1][1]==0){
//		tmp[1][1]=0;
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2){
		tot=2;
		FOR(i,2,n)t[2]=i,ans=min(ans,solve());
	}
	if(m>=3){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=0x3f3f3f3f;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			FOR(j,i+1,5)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		ins(i,j,a[i][k]+a[k][j],k);
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
//	if(tmp[1][1]==0){
//		tmp[1][1]=0;
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1;
	if(m>=2){
		tot=2;
		FOR(i,2,n)t[2]=i,ans=min(ans,solve());
	}
	if(m>=3){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
//	cout<<ans<<'\n';
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
//	if(tmp[1][1]==0){
//		cout<<tot<<'\n';
//		FOR(i,1,tot)cout<<t[i]<<' ';
//		exit(0);
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
	cout<<ans<<'\n';
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
	if(tmp[1][1]==0){
		cout<<tot<<'\n';
		FOR(i,1,tot)cout<<t[i]<<' ';
		exit(0);
	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
//	if(tmp[1][1]==0){
//		tmp[1][1]=0;
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			FOR(j,i+1,5)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		ins(i,j,a[i][k]+a[k][j],k);
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
//	if(tmp[1][1]==0){
//		tmp[1][1]=0;
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
//	FOR(i,1,m-1){
//		FOR(j,1,n)a[j][j]=INF;
//		mul();
//	}
//	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
//	cout<<ans<<'\n';
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
	FOR(i,1,tot)tag[t[i]]=0;
//	if(tmp[1][1]==0){
//		cout<<tot<<'\n';
//		FOR(i,1,tot)cout<<t[i]<<' ';
//		exit(0);
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1,ans=0x7fffffff;
	if(m>=1&&n>m){
		tot=1,ans=solve();
	}
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	if(m>=5&&n>m){
		tot=5;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)FOR(p,k+1,n)
			t[2]=i,t[3]=j,t[4]=k,t[5]=p,ans=min(ans,solve());

	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	static const int flag[2]={1,-1};
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))_f=flag[_ch=='-'],_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e2+1,INF=1e8+1;
int n,m,ans;
int a[N][N],b[N][N];
int c[N][N][6],d[N][N][6];
V input(){
	n=getint(),m=getint();
	FOR(i,1,n)FOR(j,1,n)a[i][j]=b[i][j]=getint();
}
V mul(){
	static int C[N][N];
	FOR(i,1,n)FOR(j,1,n)C[i][j]=INF;
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
		C[i][j]=min(C[i][j],a[i][k]+b[k][j]);
	FOR(i,1,n)FOR(j,1,n)a[i][j]=C[i][j];
}
V ins(int x,int y,int z,int p){
	FOR(i,0,5)
		if(!d[x][y][i]){
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
		else if(z<c[x][y][i]){
			ROF(j,5,i+1)
				c[x][y][j]=c[x][y][j-1],d[x][y][j]=d[x][y][j-1];
			c[x][y][i]=z,d[x][y][i]=p;
			return;
		}
}
V init(){
	FOR(i,1,n)a[i][i]=b[i][i]=INF;
	FOR(i,1,m-1){
		FOR(j,1,n)a[j][j]=INF;
		mul();
	}
	ans=a[1][1];
	FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)if(k!=i&&k!=j)
		ins(i,j,a[i][k]+a[k][j],k);
//	cout<<ans<<'\n';
}
int t[N],tot,tag[N];
int tmp[N][N],bas[N][N];
I getmin(int x,int y){
	FOR(i,0,5)if(!tag[d[x][y][i]])
		return c[x][y][i];
//	return INF;
}
V MUL(){
	static int C[N][N];
	FOR(i,1,tot)FOR(j,1,tot)C[i][j]=INF;
	FOR(i,1,tot)FOR(j,1,tot)FOR(k,1,tot)
		C[i][j]=min(C[i][j],tmp[i][k]+bas[k][j]);
	FOR(i,1,tot)FOR(j,1,tot)tmp[i][j]=C[i][j];
}
I solve(){
	FOR(i,1,tot)tag[t[i]]=1;
	FOR(i,1,tot)FOR(j,1,tot)
		tmp[i][j]=bas[i][j]=getmin(t[i],t[j]);
	FOR(i,1,m-1)MUL();
	FOR(i,1,tot)tag[t[i]]=0;
//	if(tmp[1][1]==0){
//		cout<<tot<<'\n';
//		FOR(i,1,tot)cout<<t[i]<<' ';
//		exit(0);
//	}
	return tmp[1][1];
}
V work(){
	m>>=1,t[1]=1,tag[0]=1;
	if(m>=2&&n>m){
		tot=2;
		FOR(i,2,n)
			t[2]=i,ans=min(ans,solve());
	}
	if(m>=3&&n>m){
		tot=3;
		FOR(i,2,n)FOR(j,i+1,n)
			t[2]=i,t[3]=j,ans=min(ans,solve());
	}
	if(m>=4&&n>m){
		tot=4;
		FOR(i,2,n)FOR(j,i+1,n)FOR(k,j+1,n)
			t[2]=i,t[3]=j,t[4]=k,ans=min(ans,solve());
	}
	cout<<ans<<'\n';

}
int main(){
//	freopen("test.in","r",stdin);
	input();
	init();
	work();
	return 0;
}


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

// missed charactors / ?

template <typename T>
boolean vmin(T& a, T b) {
	return (a > b) ? (a = b, true) : (false);
}
template <typename T>
boolean vmax(T& a, T b) {
	return (a < b) ? (a = b, true) : (false);
}

template <typename T>
T smax(T x) {
	return x;
}
template <typename T, typename ...K>
T smax(T a, const K &...args) {
	return max(a, smax(args...));
}

template <typename T>
T smin(T x) {
	return x;
}
template <typename T, typename ...K>
T smin(T a, const K &...args) {
	return min(a, smin(args...));
}

// debugging lib

#define VN(x) #x
#define Vmsg(x) VN(x) << " = " << (x)
#define printv(x) cerr << VN(x) << " = " << (x);
#define debug(...) fprintf(stderr, __VA_ARGS__);

template <typename A, typename B>
ostream& operator << (ostream& os, const pair<A, B>& z) {
	os << "(" << z.first << ", " << z.second << ')';
	return os;
}
template <typename T>
ostream& operator << (ostream& os, const vector<T>& a) {
	boolean isfirst = true;
	os << "{";
	for (auto z : a) {
		if (!isfirst) {
			os << ", ";
		}
		os << z;
		isfirst = false;
	}
	os << '}';
	return os;
}

const int N = 81, Kmx = 11;

const int inf = (signed) (~0u >> 2);

int myrand() {
	const static unsigned msk = (1u << 30) - 1;
	static unsigned seed = 2333;
	return (seed = seed * seed * 7 + seed * 998244353 + 142857) & msk;
}

int n, K;
int col[N];
int G[N][N];
int f[Kmx][N][N];

int main() {
	srand((unsigned) time (NULL));
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> K;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> G[i][j];
		}
	}
	int ans = inf;
	while (clock() * 1.0 / CLOCKS_PER_SEC < 2) {
		for (int i = 1; i <= n; i++) {
			col[i] = rand() & 1;
		}
		for (int i = 0; i <= K; i++) {
			for (int j = 1; j <= n; j++) {
				for (int k = 1; k <= n; k++) {
					f[i][j][k] = inf;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			f[0][i][i] = 0;
		}
		for (int i = 0; i < K; i++) {
			for (int u = 1; u <= n; u++) {
				for (int v = 1; v <= n; v++) {
					int g = f[i][u][v];
					if (g == inf)
						continue;
					for (int e = 1; e <= n; e++) {
						if (col[v] ^ col[e]) {
							vmin(f[i + 1][u][e], g + G[v][e]);
						}
					}
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			vmin(ans, f[K][i][i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


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

// missed charactors / ?

template <typename T>
boolean vmin(T& a, T b) {
	return (a > b) ? (a = b, true) : (false);
}
template <typename T>
boolean vmax(T& a, T b) {
	return (a < b) ? (a = b, true) : (false);
}

template <typename T>
T smax(T x) {
	return x;
}
template <typename T, typename ...K>
T smax(T a, const K &...args) {
	return max(a, smax(args...));
}

template <typename T>
T smin(T x) {
	return x;
}
template <typename T, typename ...K>
T smin(T a, const K &...args) {
	return min(a, smin(args...));
}

// debugging lib

#define VN(x) #x
#define Vmsg(x) VN(x) << " = " << (x)
#define printv(x) cerr << VN(x) << " = " << (x);
#define debug(...) fprintf(stderr, __VA_ARGS__);

template <typename A, typename B>
ostream& operator << (ostream& os, const pair<A, B>& z) {
	os << "(" << z.first << ", " << z.second << ')';
	return os;
}
template <typename T>
ostream& operator << (ostream& os, const vector<T>& a) {
	boolean isfirst = true;
	os << "{";
	for (auto z : a) {
		if (!isfirst) {
			os << ", ";
		}
		os << z;
		isfirst = false;
	}
	os << '}';
	return os;
}

const int N = 81, Kmx = 11;

const int inf = (signed) (~0u >> 2);

int myrand() {
	const static unsigned msk = (1u << 30) - 1;
	static unsigned seed = 2333;
	return (seed = seed * seed * 7 + seed * 998244353 + 142857) & msk;
}

int n, K;
int col[N];
int G[N][N];
int f[Kmx][N][N];

int main() {
	srand((unsigned) time (NULL));
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> K;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> G[i][j];
		}
	}
	int ans = inf;
	while (clock() * 1.0 / CLOCKS_PER_SEC < 2.7) {
		vector<int> V[2];
		for (int i = 1; i <= n; i++) {
			col[i] = rand() & 1;
			V[col[i]].push_back(i);
		}
		for (int i = 0; i <= K; i++) {
			for (int j = 1; j <= n; j++) {
				for (int k = 1; k <= n; k++) {
					f[i][j][k] = inf;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			f[0][i][i] = 0;
		}
		for (int i = 0; i < K; i++) {
			for (int u = 1; u <= n; u++) {
				for (int v = 1; v <= n; v++) {
					int g = f[i][u][v];
					if (g == inf)
						continue;
					for (auto e : V[col[v] ^ 1]) {
						vmin(f[i + 1][u][e], g + G[v][e]);
					}
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			vmin(ans, f[K][i][i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


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

// missed charactors / ?

template <typename T>
boolean vmin(T& a, T b) {
	return (a > b) ? (a = b, true) : (false);
}
template <typename T>
boolean vmax(T& a, T b) {
	return (a < b) ? (a = b, true) : (false);
}

template <typename T>
T smax(T x) {
	return x;
}
template <typename T, typename ...K>
T smax(T a, const K &...args) {
	return max(a, smax(args...));
}

template <typename T>
T smin(T x) {
	return x;
}
template <typename T, typename ...K>
T smin(T a, const K &...args) {
	return min(a, smin(args...));
}

// debugging lib

#define VN(x) #x
#define Vmsg(x) VN(x) << " = " << (x)
#define printv(x) cerr << VN(x) << " = " << (x);
#define debug(...) fprintf(stderr, __VA_ARGS__);

template <typename A, typename B>
ostream& operator << (ostream& os, const pair<A, B>& z) {
	os << "(" << z.first << ", " << z.second << ')';
	return os;
}
template <typename T>
ostream& operator << (ostream& os, const vector<T>& a) {
	boolean isfirst = true;
	os << "{";
	for (auto z : a) {
		if (!isfirst) {
			os << ", ";
		}
		os << z;
		isfirst = false;
	}
	os << '}';
	return os;
}

const int N = 81, Kmx = 11;

const int inf = (signed) (~0u >> 2);

int myrand() {
	const static unsigned msk = (1u << 30) - 1;
	static unsigned seed = 2333;
	return (seed = seed * seed * 7 + seed * 998244353 + 142857) & msk;
}

int n, K;
int col[N];
int G[N][N];
int f[Kmx][N][N];

int main() {
	srand((unsigned) time (NULL));
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> K;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> G[i][j];
		}
	}
	int ans = inf;
	while (clock() * 1.0 / CLOCKS_PER_SEC < 2.7) {
		vector<int> V[2];
		for (int i = 1; i <= n; i++) {
			col[i] = rand() & 1;
			V[col[i]].push_back(i);
		}
		for (int i = 0; i <= K; i++) {
			for (int j = 1; j <= n; j++) {
				for (int k = 1; k <= n; k++) {
					f[i][j][k] = inf;
				}
			}
		}
		f[0][1][1] = 0;
		for (int i = 0; i < K; i++) {
			for (int u = 1; u <= n; u++) {
				for (int v = 1; v <= n; v++) {
					int g = f[i][u][v];
					if (g == inf)
						continue;
					for (auto e : V[col[v] ^ 1]) {
						vmin(f[i + 1][u][e], g + G[v][e]);
					}
				}
			}
		}
		vmin(ans, f[K][1][1]);
	}
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 80+1, K = 10+1;

int n, k;
int cost[N][N];
vector<pair<int, int>> cost2[N][N];

int ans = 1e9;
int p[K];
bool used[N];
void check(){
    int blocked = 0;
    for(int i = 0; i < n; ++i)
        used[i] = false;
    for(int i = 0; i < k/2; ++i){
        blocked += !used[p[i]];
        used[p[i]] = true;
    }
    if(blocked == n)
        return;
    int sum = 0;
    for(int i = 0; i < k/2; ++i){
        int j = 0;
        while(used[cost2[p[i]][p[i+1]][j].second])
            ++j;
        sum += cost2[p[i]][p[i+1]][j].first;
    }
    ans = min(ans, sum);
}
void rec(int pos=1){
    if(pos == k/2){
        check();
        return;
    }
    for(int i = 0; i < n; ++i){
        p[pos] = i;
        rec(pos+1);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            cin >> cost[i][j];
    for(int a = 0; a < n; ++a)
        for(int b = 0; b < n; ++b){
            for(int c = 0; c < n; ++c) if(c != a && c != b)
                cost2[a][b].push_back({cost[a][c]+cost[c][b], c});
            sort(cost2[a][b].begin(), cost2[a][b].end());
        }
    p[0] = 0;
    p[k/2+1] = 0;
    rec();
    cout << ans << '\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n , k;
int w[100][100];
vector<pii> d[100][100];
bool vis[100];
ll ans = 1e18;
void dfs(int cnt,vi now){
	if(cnt == 0 ){
		ll res = 0 ;
		int pre = 1;
		now.pb(1);
		for(auto v:now)
			vis[v] = true;
		for(auto v:now){
			int cc = 0;
			for(auto it:d[pre][v]){
				if(!vis[it.se]){
					res += it.fi;
					cc = 1;
					break;
				}
			}
			if(!cc){
				res = 1e18;
				break;
			}
			pre = v;
		}
		ans = min(ans,res);
		now.pop_back();
		for(auto v:now)
			vis[v] = false;
		return;
	}
	rep(i , 1 , n + 1){
		now.pb(i);
		dfs(cnt - 1,now);
		now.pop_back();
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>k;
	rep( i  , 1 , n + 1)
		rep( j , 1 , n + 1)
			cin>>w[i][j];
	rep(i , 1 , n + 1)
		rep(j , 1 , n + 1){
			rep( k , 1 , n + 1){
				if(i == k || j == k )continue;
				d[i][j].pb(pii(w[i][k] + w[k][j],k));
			}
			sort(all(d[i][j]));
		}
	vi t;
	dfs(k/2 - 1,t);
	cout<<ans<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n , k;
int w[100][100];
vector<pii> d[100][100];
int c[100];
int ans = 1e9;
void dfs(int cnt,vi now){
	if(cnt == 0 ){
		int res = 0 ;
		int pre = 1;
		now.pb(1);
		for(auto v:now){
			int cc = 0;
			for(auto it:d[pre][v]){
				if(!c[it.se]){
					res += it.fi;
					cc = 1;
					break;
				}
			}
			if(!cc){
				now.pop_back();
				return;
			}
			pre = v;
		}
		ans = min(ans,res);
		now.pop_back();
		return;
	}
	rep(i , 1 , n + 1){
		now.pb(i);
		c[i]++;
		dfs(cnt - 1,now);
		now.pop_back();
		c[i]--;
	}
}
int main()
{

	scanf("%d%d",&n,&k);
	rep( i  , 1 , n + 1)
		rep( j , 1 , n + 1)
			scanf("%d",&w[i][j]);
	rep(i , 1 , n + 1)
		rep(j , 1 , n + 1){
			rep( k , 1 , n + 1){
				if(i == k || j == k )continue;
				d[i][j].pb(pii(w[i][k] + w[k][j],k));
			}
			sort(all(d[i][j]));
		}
	vi t;
	dfs(k/2 - 1,t);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n ;
pii p[N];
priority_queue<int> q;
int main()
{
	cin>>n;
	rep(i , 1 , n + 1)
		cin>>p[i].fi;
	rep(i , 1 , n + 1)
		cin>>p[i].se;
	sort(p + 1,p + n + 1);
	ll ans = 0,ss = 0;
	p[0].fi = p[1].fi;
	p[n + 1].fi = 2e9;
	rep(i , 1 , n + 2){
		int d = p[i].fi - p[i - 1].fi;
		for(int j = 0;j<d && !q.empty();++j){
			ll x = q.top();
			q.pop();
			ss -= x;
			ans += x*j;
		}
		q.push(p[i].se);
		ans += ss*d;
		ss += p[i].se;
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n , k;
int w[100][100];
vector<pii> d[100][100];
int c[100];
int ans = 1e9;
vi now;
void dfs(int cnt){
	if(cnt == 0 ){
		int res = 0 ;
		int pre = 1;
		now.pb(1);
		for(auto v:now){
			int cc = 0;
			for(auto it:d[pre][v]){
				if(!c[it.se]){
					res += it.fi;
					cc = 1;
					break;
				}
			}
			if(!cc || res >= ans){
				now.pop_back();
				return;
			}
			pre = v;
		}
		ans = min(ans,res);
		now.pop_back();
		return;
	}
	rep(i , 1 , n + 1){
		now.pb(i);
		c[i]++;
		dfs(cnt - 1);
		now.pop_back();
		c[i]--;
	}
}
int main()
{

	scanf("%d%d",&n,&k);
	rep( i  , 1 , n + 1)
		rep( j , 1 , n + 1)
			scanf("%d",&w[i][j]);
	rep(i , 1 , n + 1)
		rep(j , 1 , n + 1){
			rep( k , 1 , n + 1){
				if(i == k || j == k )continue;
				d[i][j].pb(pii(w[i][k] + w[k][j],k));
			}
			sort(all(d[i][j]));
		}
	dfs(k/2 - 1);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
typedef vector<int>vi;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fi first
#define se second
#define de(x) cout<<#x<<"="<<x<<"\n"
#define dd(x) cout<<#x<<"="<<x<<" "
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lowbit(a) ((a)&-(a))
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
const int N=2e6+5;
int n , k;
int w[100][100];
vector<pii> d[100][100];
int c[100];
int ans = 1e9;
void dfs(int cnt,vi &now){
	if(cnt == 0 ){
		int res = 0 ;
		int pre = 1;
		now.pb(1);
		for(auto v:now){
			int cc = 0;
			for(auto it:d[pre][v]){
				if(!c[it.se]){
					res += it.fi;
					cc = 1;
					break;
				}
			}
			if(!cc || res >= ans){
				now.pop_back();
				return;
			}
			pre = v;
		}
		ans = min(ans,res);
		now.pop_back();
		return;
	}
	rep(i , 1 , n + 1){
		now.pb(i);
		c[i]++;
		dfs(cnt - 1,now);
		now.pop_back();
		c[i]--;
	}
}
int main()
{

	scanf("%d%d",&n,&k);
	rep( i  , 1 , n + 1)
		rep( j , 1 , n + 1)
			scanf("%d",&w[i][j]);
	rep(i , 1 , n + 1)
		rep(j , 1 , n + 1){
			rep( k , 1 , n + 1){
				if(i == k || j == k )continue;
				d[i][j].pb(pii(w[i][k] + w[k][j],k));
			}
			sort(all(d[i][j]));
		}
	vi t;
	dfs(k/2 - 1,t);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[101][101], b[101][101], s[101], p[101];
const int inf = 0x3f3f3f3f;

int n, K;
int ans = 0x3f3f3f3f;

void multiply() {
	for (int i = 1; i <= n; i ++) {
		s[i] = p[i]; p[i] = inf;
	}
	for (int k = 1; k <= n; k ++)
		for (int j = 1; j <= n; j ++)
			p[j] = min(p[j], s[k] + b[k][j]);
}
int col[1001];
int main( ) {
	scanf("%d %d", &n, &K);
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++)
			scanf("%d", &a[i][j]);

	for (int T = 1; T <= 10000; ++ T) {
		for (int i = 1; i <= n; i ++) col[i] = rand() % 2;
		for (int i = 1; i <= n; i ++)
			for (int j = 1; j <= n; j ++) {
				if (i != j) {
					if (col[i] == col[j]) b[i][j] = inf;
					else b[i][j] = a[i][j];
				}
				else b[i][j] = inf;
				if (i == 1) p[j] = b[i][j];
			}
		for (int i = 1; i < K; i ++) {
			multiply();
		}
		ans = min(ans, p[1]);
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[101][101], b[101][101], s[101], p[101];
const int inf = 0x3f3f3f3f;

int n, K;
int ans = 0x3f3f3f3f;

void multiply() {
	for (int i = 1; i <= n; i ++) {
		s[i] = p[i]; p[i] = inf;
	}
	for (int k = 1; k <= n; k ++)
		for (int j = 1; j <= n; j ++)
			p[j] = min(p[j], s[k] + b[k][j]);
}
int col[1001];
int main( ) {
	srand(23334);
	scanf("%d %d", &n, &K);
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++)
			scanf("%d", &a[i][j]);

	for (int T = 1; T <= 20000; ++ T) {
		for (int i = 1; i <= n; i ++) col[i] = rand() % 2;
		for (int i = 1; i <= n; i ++)
			for (int j = 1; j <= n; j ++) {
				if (i != j) {
					if (col[i] == col[j]) b[i][j] = inf;
					else b[i][j] = a[i][j];
				}
				else b[i][j] = inf;
				if (i == 1) p[j] = b[i][j];
			}
		for (int i = 1; i < K; i ++) {
			multiply();
		}
		ans = min(ans, p[1]);
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int N=85,K=15;
int a[N][N],ans=2e9,f[N][K],n,k;
bool col[N];

void doit(){
	for(int i=1;i<=n;i++) col[i]=rand()&1;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=k;j++) f[i][j]=2e9;
	f[1][0]=0;
	for(int i=0;i<k;i++)
		for(int x=1;x<=n;x++) if(f[x][i]!=2e9)
			for(int y=1;y<=n;y++) if(col[x]^col[y])
				f[y][i+1]=min(f[y][i+1],f[x][i]+a[x][y]);
	ans=min(ans,f[1][k]);
}

signed main(){
	srand(time(NULL));
	read(n);read(k);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) read(a[i][j]);
	while(clock()<CLOCKS_PER_SEC*2.85) doit();
	write(ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int P = 998244353;
const LL INF = 0x3f3f3f3f;
const int N = 81;

int n, k;
int col[N];
LL M[N][N], A[N][N], f[N][N], final_ans = INF;

void work() {
    for(int i = 1; i <= n; i++) col[i] = rand() % 2;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(col[i] != col[j]) A[i][j] = M[i][j];
            else A[i][j] = INF;
        }
    }
    //for(int i = 1; i <= k; i++) for(int j = 1; j <= n; j++) f[i][j] = 0;
    for(int i = 1; i <= n; i++) f[1][i] = INF;
    f[1][1] = 0;
    for(int i = 2; i <= k; i++) {
        for(int j = 1; j <= n; j++) {
            f[i][j] = INF;
            for(int t = 1; t <= n; t++)
                f[i][j] = min(f[i][j], f[i - 1][t] + A[t][j]);
        }
    }
    LL ans = INF;
    for(int i = 1; i <= n; i++) ans = min(ans, f[k][i] + A[i][1]);
    final_ans = min(final_ans, ans);
}

void init() {
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            int x;
            cin >> x;
            if(i == j) M[i][j] = INF;
            else M[i][j] = x;
        }
    }
    for(int i = 1; i <= 4999; i++) {
        work();
    }
    cout << final_ans << endl;
}

int main() {
    srand(time(0));
    init();
    return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<iii> viii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi &cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    unordered_set<ll> no(cur.begin(), cur.end());

    priority_queue<iii, viii, greater<iii>> pq;
    vvi dist(cur.size(), vi(k+1, INF));
    dist[0][0] = 0;
    pq.push(iii(0, ii(0, 0)));

    while (true) {
      iii u = pq.top(); pq.pop();
      if (u.s.f == 0 && u.s.s == k) {
        return u.f;
      }

      if (dist[u.s.f][u.s.s] == u.f) {
        for (ll i = 0; i < cur.size(); ++i) {
          for (ll j = 0; true; ++j) {
            if (!no.count(fastest_ways[cur[u.s.f]][cur[i]][j].s)) {
              ll cost = fastest_ways[cur[u.s.f]][cur[i]][j].f + dist[u.s.f][u.s.s];
              if (cost < dist[i][u.s.s+2]) {
                dist[i][u.s.s+2] = cost;
                pq.push(iii(cost, ii(i, u.s.s+2)));
              }
              break;
            }
          }
        }
      }
    }
  } else {
    ll best = INF;
    best = min(best, choose_route(cur, steps_remaining-2));

    for (ll i = cur.back()+1; i < n; ++i) {
      cur.emplace_back(i);
      best = min(best, choose_route(cur, steps_remaining-2));
      cur.pop_back();
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  vi result;
  result.push_back(0);
  cout << choose_route(result, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<iii> viii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi &cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    unordered_set<ll> no(cur.begin(), cur.end());

    priority_queue<iii, viii, greater<iii>> pq;
    vvi dist(cur.size(), vi(k+1, INF));
    dist[0][0] = 0;
    pq.push(iii(0, ii(0, 0)));

    while (true) {
      iii u = pq.top(); pq.pop();
      if (u.s.f == 0 && u.s.s == k) {
        return u.f;
      }

      if (dist[u.s.f][u.s.s] == u.f) {
        for (ll i = 0; i < cur.size(); ++i) {
          for (ll j = 0; true; ++j) {
            if (!no.count(fastest_ways[cur[u.s.f]][cur[i]][j].s)) {
              ll cost = fastest_ways[cur[u.s.f]][cur[i]][j].f + dist[u.s.f][u.s.s];
              if (u.s.s+2 <= k && cost < dist[i][u.s.s+2]) {
                dist[i][u.s.s+2] = cost;
                pq.push(iii(cost, ii(i, u.s.s+2)));
              }
              break;
            }
          }
        }
      }
    }
  } else {
    ll best = INF;
    best = min(best, choose_route(cur, steps_remaining-2));

    for (ll i = cur.back()+1; i < n; ++i) {
      cur.emplace_back(i);
      best = min(best, choose_route(cur, steps_remaining-2));
      cur.pop_back();
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  vi result;
  result.push_back(0);
  cout << choose_route(result, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;
ll cur[6];
ll no[85];

ll choose_route(ll len, ll steps_remaining) {
  if (steps_remaining == 0) {
    ll cost = 0;

    cur[len++] = 0;

    for (ll i = 0; i < len-1; ++i) {
      for (ll j = 0; true; ++j) {
        if (j == n) {
          return INF;
        }
        if (no[fastest_ways[cur[i]][cur[i+1]][j].s] == 0) {
          cost += fastest_ways[cur[i]][cur[i+1]][j].f;
          break;
        }
      }
    }

    return cost;
  } else {
    ll best = choose_route(len+1, steps_remaining-2);
    for (ll i = 0; i < n; ++i) {
      cur[len] = i;
      ++no[i];
      best = min(best, choose_route(len+1, steps_remaining-2));
      --no[i];
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  fill(no, no+85, 0);
  no[0] = 2;
  cur[0] = 0;
  cout << choose_route(1, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    ll cost = 0;

    cur.insert(cur.begin(), 0);
    cur.emplace_back(0);
    unordered_set<ll> no(cur.begin(), cur.end());

    for (ll i = 0; i < cur.size()-1; ++i) {
      for (ll j = 0; true; ++j) {
        if (!no.count(fastest_ways[cur[i]][cur[i+1]][j].s)) {
          cost += fastest_ways[cur[i]][cur[i+1]][j].f;
          break;
        }
      }
    }

    if (cost == 1) {
      cerr << "asdf" << endl;
    }

    return cost;
  } else {
    ll best = INF;
    for (ll i = 0; i < n; ++i) {
      vi copy_cur = cur;
      copy_cur.emplace_back(i);
      best = min(best, choose_route(copy_cur, steps_remaining-2));
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  cout << choose_route(vi(), k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;
ll cur[6];
ll no[85];

ll choose_route(ll len, ll steps_remaining) {
  if (steps_remaining == 0) {
    ll cost = 0;

    cur[len++] = 0;

    for (ll i = 0; i < len-1; ++i) {
      for (ll j = 0; true; ++j) {
        if (no[fastest_ways[cur[i]][cur[i+1]][j].s] == 0) {
          cost += fastest_ways[cur[i]][cur[i+1]][j].f;
          break;
        }
      }
    }

    return cost;
  } else {
    ll best = choose_route(len+1, steps_remaining-2);
    for (ll i = 0; i < n; ++i) {
      cur[len] = i;
      ++no[i];
      best = min(best, choose_route(len+1, steps_remaining-2));
      --no[i];
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  fill(no, no+85, 0);
  cur[0] = 0;
  cout << choose_route(1, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi &cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    ll cost = 0;

    cur.insert(cur.begin(), 0);
    cur.emplace_back(0);
    unordered_set<ll> no(cur.begin(), cur.end());

    for (ll i = 0; i < cur.size()-1; ++i) {
      for (ll j = 0; true; ++j) {
        if (!no.count(fastest_ways[cur[i]][cur[i+1]][j].s)) {
          cost += fastest_ways[cur[i]][cur[i+1]][j].f;
          break;
        }
      }
    }

    return cost;
  } else {
    ll best = INF;
    for (ll i = 0; i < n; ++i) {
      cur.emplace_back(i);
      best = min(best, choose_route(cur, steps_remaining-2));
      cur.pop_back();
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  vi result;
  cout << choose_route(result, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<iii> viii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi &cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    unordered_set<ll> no(cur.begin(), cur.end());

    priority_queue<iii, viii, greater<iii>> pq;
    vvi dist(cur.size(), vi(k+1, INF));
    dist[0][0] = 0;
    pq.push(iii(0, ii(0, 0)));

    vvi adj_matrix(cur.size(), vi(cur.size()));
    for (ll i = 0; i < cur.size(); ++i) {
      for (ll j = 0; j < cur.size(); ++j) {
        for (ll k = 0; true; ++k) {
          if (!no.count(fastest_ways[cur[i]][cur[j]][k].s)) {
            adj_matrix[i][j] = fastest_ways[cur[i]][cur[j]][k].f;
            break;
          }
        }
      }
    }

    while (true) {
      iii u = pq.top(); pq.pop();
      if (u.s.f == 0 && u.s.s == k) {
        return u.f;
      }

      if (dist[u.s.f][u.s.s] == u.f) {
        for (ll i = 0; i < cur.size(); ++i) {
          ll cost = adj_matrix[u.s.f][i] + dist[u.s.f][u.s.s];
          if (u.s.s+2 <= k && cost < dist[i][u.s.s+2]) {
            dist[i][u.s.s+2] = cost;
            pq.push(iii(cost, ii(i, u.s.s+2)));
          }
        }
      }
    }
  } else {
    ll best = INF;
    best = min(best, choose_route(cur, steps_remaining-2));

    for (ll i = cur.back()+1; i < n; ++i) {
      cur.emplace_back(i);
      best = min(best, choose_route(cur, steps_remaining-2));
      cur.pop_back();
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  vi result;
  result.push_back(0);
  cout << choose_route(result, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi &cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    ll cost = 0;

    vi new_cur(cur);
    new_cur.insert(new_cur.begin(), 0);
    new_cur.emplace_back(0);
    unordered_set<ll> no(new_cur.begin(), new_cur.end());

    for (ll i = 0; i < new_cur.size()-1; ++i) {
      for (ll j = 0; true; ++j) {
        if (!no.count(fastest_ways[new_cur[i]][new_cur[i+1]][j].s)) {
          cost += fastest_ways[new_cur[i]][new_cur[i+1]][j].f;
          break;
        }
      }
    }

    return cost;
  } else {
    ll best = INF;
    for (ll i = 0; i < n; ++i) {
      cur.emplace_back(i);
      best = min(best, choose_route(cur, steps_remaining-2));
      cur.pop_back();
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  vi result;
  cout << choose_route(result, k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;

ll choose_route(vi cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    ll cost = 0;

    cur.insert(cur.begin(), 0);
    cur.emplace_back(0);
    unordered_set<ll> no(cur.begin(), cur.end());

    for (ll i = 0; i < cur.size()-1; ++i) {
      for (ll j = 0; true; ++j) {
        if (!no.count(fastest_ways[cur[i]][cur[i+1]][j].s)) {
          cost += fastest_ways[cur[i]][cur[i+1]][j].f;
          break;
        }
      }
    }

    return cost;
  } else {
    ll best = INF;
    for (ll i = 0; i < n; ++i) {
      vi copy_cur = cur;
      copy_cur.emplace_back(i);
      best = min(best, choose_route(copy_cur, steps_remaining-2));
    }
    return best;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  cout << choose_route(vi(), k-2) << endl;

  return 0;
}


//
//  D.cpp
//  Codeforces
//
//  Created by Bryce Sandlund on 3/16/20.
//  Copyright © 2020 Bryce Sandlund. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <sstream>
#include <list>
#include <map>
#include <fstream>
#include <string>
#include <time.h>
#include <queue>
#include <tuple>
#include <functional>
#include <unordered_set>
#include <unordered_map>

// 10^9
#define INF 1000000000000
// 10^18
//#define INF 1000000000000000000LL
#define f first
#define s second
// Useful for thinking about points, commented to avoid name collision.
//#define x first
//#define y second
#define EP 1e-6

using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<iii> viii;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vb> vvb;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvii> vvvii;
typedef vector<double> vd;
typedef vector<vd> vvd;

ll n, k;
vvi adj_matrix;
vvvii fastest_ways;
ll best = INF;

void choose_route(vi &cur, ll steps_remaining) {
  if (steps_remaining == 0) {
    unordered_set<ll> no(cur.begin(), cur.end());

    priority_queue<iii, viii, greater<iii>> pq;
    vvi dist(cur.size(), vi(k+1, INF));
    dist[0][0] = 0;
    pq.push(iii(0, ii(0, 0)));

    vvi adj_matrix(cur.size(), vi(cur.size()));
    for (ll i = 0; i < cur.size(); ++i) {
      for (ll j = 0; j < cur.size(); ++j) {
        for (ll k = 0; true; ++k) {
          if (!no.count(fastest_ways[cur[i]][cur[j]][k].s)) {
            adj_matrix[i][j] = fastest_ways[cur[i]][cur[j]][k].f;
            break;
          }
        }
      }
    }

    while (true) {
      iii u = pq.top(); pq.pop();
      if (u.f > best) {
        break;
      }

      if (u.s.f == 0 && u.s.s == k) {
        best = min(best, u.f);
        break;
      }

      if (dist[u.s.f][u.s.s] == u.f) {
        for (ll i = 0; i < cur.size(); ++i) {
          ll cost = adj_matrix[u.s.f][i] + dist[u.s.f][u.s.s];
          if (u.s.s+2 <= k && cost < dist[i][u.s.s+2]) {
            dist[i][u.s.s+2] = cost;
            pq.push(iii(cost, ii(i, u.s.s+2)));
          }
        }
      }
    }
  } else {
    choose_route(cur, steps_remaining-2);

    for (ll i = cur.back()+1; i < n; ++i) {
      cur.emplace_back(i);
      choose_route(cur, steps_remaining-2);
      cur.pop_back();
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  adj_matrix = vvi(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> adj_matrix[i][j];
      if (i == j)
        adj_matrix[i][j] = INF;
    }
  }

  fastest_ways = vvvii(n, vvii(n, vii(n)));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        fastest_ways[i][j][k] = ii(adj_matrix[i][k] + adj_matrix[k][j], k);
      }
      sort(fastest_ways[i][j].begin(), fastest_ways[i][j].end());
    }
  }

  vi result;
  result.push_back(0);
  choose_route(result, k-2);
  cout << best << endl;

  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=85;
int n,K,ans,fl[N];
int f[2][N],a[N][N];
void solve(){
	For(i,1,n) fl[i]=rand()&1;
	Rep(i,n,1) fl[i]^=fl[1];
	memset(f,100,sizeof(f));
	f[0][1]=0;
	For(i,1,K){
		memset(f[i&1],100,sizeof(f[i&1]));
		For(j,1,n) if ((fl[j]+i+1)&1)
			For(k,1,n) if ((fl[k]+i)&1)
				f[i&1][j]=min(f[i&1][j],f[(i&1)^1][k]+a[k][j]);
	}
	ans=min(ans,f[0][1]);
}
int main(){
	srand(time(NULL));
	ans=1<<30;
	scanf("%d%d",&n,&K);
	For(i,1,n) For(j,1,n) scanf("%d",&a[i][j]);
	for (;1.0*clock()/CLOCKS_PER_SEC<=1.0;solve());
	printf("%d\n",ans);
}
/*
搜索+dp?
随机化!
*/


#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#if 0
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define int long long
const int inf=1e9+10;
int dis[85][12];
int mat[85][85];
vector<int> eve;
vector<int> odd;
int n,k;
int go(){
    for(int i=1;i<=n;i++)for(int j=0;j<=k;j++)dis[i][j]=inf;
    dis[1][0]=0;
    for(int r=0;r<k/2;r++){
        for(int tar : odd){
            for(int from : eve){
                dis[tar][r*2+1]=min(dis[tar][r*2+1],dis[from][r*2]+mat[from][tar]);
                //debug(dis[tar][r*2+1]);
            }
        }
        for(int tar : eve){
            for(int from : odd){
                dis[tar][r*2+2]=min(dis[tar][r*2+2],dis[from][r*2+1]+mat[from][tar]);
                //debug(dis[tar][r*2+2]);
            }
        }
    }
    debug(dis[1][k]);
    return dis[1][k];
}
int32_t main(){
    srand(time(0));
    cin >>n >>k;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin >>mat[i][j];
        }
    }
    int ans=inf;
    vector<int> missing;
    for(int i=2;i<=n;i++)missing.push_back(i);
    for(int z=0;z<10000;z++){
        eve={1};
        odd={};
        random_shuffle(missing.begin(),missing.end());
        for(int i : missing){
            if(rand()&1){
                eve.push_back(i);
            }
            else{
                odd.push_back(i);
            }
        }
        debug(eve,odd);
        ans=min(ans,go());
    }
    cout <<ans;
}


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

const int maxn = 111;
const int MAGIC = 100000;
const int inf = 0x3f3f3f3f;
int n, k, col[maxn], a[maxn][maxn], ans;

mt19937 rng;
int dp[maxn], ndp[maxn];
void solve()
{
	for(int i=1; i<n; i++) col[i] = static_cast<unsigned>(rng()) % 2;
	memset(dp, inf, sizeof(dp));
	dp[0] = 0;
	for(int i=0; i<k; i++)
	{
		memset(ndp, inf, sizeof(ndp));
		for(int j=0; j<n; j++) for(int k=0; k<n; k++) if(col[j] != col[k])
			ndp[k] = min(ndp[k], dp[j] + a[j][k]);
		memcpy(dp, ndp, sizeof(dp));
	}
	ans = min(ans, dp[0]);
}

int main()
{
	rng = mt19937(chrono::high_resolution_clock().now().time_since_epoch().count());
	n = 80; k = 10;//scanf("%d%d", &n, &k);
	//for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%d", a[i]+j);
	for(int i=0; i<n; i++) for(int j=0; j<n; j++)a[i][j] = uniform_int_distribution<int>(1, 100000000)(rng);

	ans = inf;
	for(int i=0; i<MAGIC; i++) solve();
	printf("%d\n", ans);
	return 0;
}


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

const int maxn = 111;
const int MAGIC = 100000;
const int inf = 0x3f3f3f3f;
int n, k, col[maxn], a[maxn][maxn], ans;

mt19937 rng;
int dp[maxn], ndp[maxn];
void solve()
{
	for(int i=1; i<n; i++) col[i] = static_cast<unsigned>(rng()) % 2;
	memset(dp, inf, sizeof(dp));
	dp[0] = 0;
	for(int i=0; i<k; i++)
	{
		memset(ndp, inf, sizeof(ndp));
		for(int j=0; j<n; j++) for(int k=0; k<n; k++) if(col[j] != col[k])
			ndp[k] = min(ndp[k], dp[j] + a[j][k]);
		memcpy(dp, ndp, sizeof(dp));
	}
	ans = min(ans, dp[0]);
}

int main()
{
	rng = mt19937(chrono::high_resolution_clock().now().time_since_epoch().count());
	scanf("%d%d", &n, &k);
	for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%d", a[i]+j);

	ans = inf;
	for(int i=0; i<MAGIC; i++) solve();
	printf("%d\n", ans);
	return 0;
}


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

const int maxn = 111;
const int MAGIC = 30000;
const int inf = 0x3f3f3f3f;
int n, k, col[maxn], a[maxn][maxn], ans;

mt19937 rng;
int dp[maxn], ndp[maxn];
void solve()
{
	for(int i=1; i<n; i++) col[i] = static_cast<unsigned>(rng()) % 2;
	memset(dp, inf, sizeof(dp));
	dp[0] = 0;
	for(int i=0; i<k; i++)
	{
		memset(ndp, inf, sizeof(ndp));
		for(int j=0; j<n; j++) for(int k=0; k<n; k++) if(col[j] != col[k])
			ndp[k] = min(ndp[k], dp[j] + a[j][k]);
		memcpy(dp, ndp, sizeof(dp));
	}
	ans = min(ans, dp[0]);
}

int main()
{
	rng = mt19937(chrono::high_resolution_clock().now().time_since_epoch().count());
	scanf("%d%d", &n, &k);
	for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%d", a[i]+j);

	ans = inf;
	for(int i=0; i<MAGIC; i++) solve();
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = (int)2e5 + 123, inf = 1e9, mod = 1e9 + 7;
const ll INF = 1e18;

int n, k, a[80][80], dp[80][20], par[80], ans = inf;
int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			scanf("%d", &a[i][j]);
	int m = 40000;
	while(m--){
		for(int i = 0; i < n; i++)
			par[i] = rand() & 1;
		for(int i = 0; i < n; i++)
			for(int j = 0; j <= k; j++)
				dp[i][j] = inf;
		dp[0][0] = 0;
		for(int j = 0; j < k; j++)
			for(int i = 0; i < n; i++)
				for(int q = 0; q < n; q++)
					if(par[i] != par[q])
						dp[q][j + 1] = min(dp[q][j + 1], dp[i][j] + a[i][q]);
	    ans = min(ans, dp[0][k]);
	}
	printf("%d", ans);
}


#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll ;
#define rep(i ,a, b) for (int i = a ; i <= b; ++ i)
const int N = 85, inf = 1e9 + 7 ;
using namespace std ;

bool vis[N] ;
int n, m, G[N][N], p[10], ans = inf, f[N][N], g[N][N] ;

void Pre() {
	rep(i, 1, n) rep(j, 1, n) {
		f[i][j] = inf ;
		rep(k, 1, n) if (i != k && k != j)
			if (G[i][k] + G[k][j] <= f[i][j]) {
				f[i][j] = G[i][k] + G[k][j] ;
				g[i][j] = k ;
			}
	}
}

void Solve() {
	for (int i = 1 ; i < m ; i += 2) {
		vis[p[i]] = true ;
	}
	int Sum = 0 ;
	for (int i = 1 ; i < m ; i += 2) {
		int Mn = inf, u = p[i], v = p[i + 2] ;
		if (!vis[g[u][v]]) {
			Sum += f[u][v] ;
			continue ;
		}
		rep(j, 1, n) if (!vis[j] && G[u][j] + G[j][v] < Mn)
			Mn = G[u][j] + G[j][v] ;
		Sum += Mn ;
	}
	ans = min(ans, Sum) ;
	for (int i = 1 ; i < m ; i += 2) {
		vis[p[i]] = false ;
	}
}

void dfs(int st) {
	if (st == m + 1) {
		Solve() ;
		return ;
	}
	rep(i, 1, n) {
		p[st] = i ;
		dfs(st + 2) ;
	}
}

int main() {
	scanf("%d%d", &n, &m) ;
	rep(i, 1, n) rep(j, 1, n) scanf("%d", &G[i][j]) ;
	Pre() ;
	p[1] = p[m + 1] = 1 ;
	dfs(3) ;
	printf("%d\n", ans) ;
	return 0 ;
}


#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll ;
#define rep(i ,a, b) for (int i = a ; i <= b; ++ i)
const int N = 85, inf = 1e9 + 7 ;
using namespace std ;

bool vis[N] ;
int n, m, G[N][N], p[10], ans = inf ;

void Solve() {
	for (int i = 1 ; i < m ; i += 2) {
		vis[p[i]] = true ;
	}
	int Sum = 0 ;
	for (int i = 1 ; i < m ; i += 2) {
		int Mn = inf, u = p[i], v = p[i + 2] ;
		rep(j, 1, n) if (!vis[j])
			Mn = min(Mn, G[u][j] + G[j][v]) ;
		Sum += Mn ;
	}
	ans = min(ans, Sum) ;
	for (int i = 1 ; i < m ; i += 2) {
		vis[p[i]] = false ;
	}
}

void dfs(int st) {
	if (st == m + 1) {
		Solve() ;
		return ;
	}
	rep(i, 1, n) {
		p[st] = i ;
		dfs(st + 2) ;
	}
}

int main() {
	scanf("%d%d", &n, &m) ;
	rep(i, 1, n) rep(j, 1, n) scanf("%d", &G[i][j]) ;
	p[1] = p[m + 1] = 1 ;
	dfs(3) ;
	printf("%d\n", ans) ;
	return 0 ;
}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;

int main() {
    int n, k; cin >> n >> k;
	int g[n + 1][n + 1];
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> g[i][j];
		}
	}
	int ct = 10000;
	int ans = 2e9;
	while(ct --) {
		int d[n + 1];
		d[1] = 0;
		bool groups[n + 1];
		groups[1] = true;
		for(int i = 2; i <= n; i++) {
			d[i] = 1e9 + 1;
			groups[i] = (rand() % 2);
			// trace(i, groups[i]);
		}
		for(int kc = 1; kc <= k; kc++) {
			int nd[n + 1];
			for(int i = 1; i <= n; i++) {
				int md = 1e9 + 1;
				for(int j = 1; j <= n; j++) {
					bool x = groups[i] ^ groups[j];
					if(x) {
						// trace(x, i, j, d[j] + g[j][i]);
						md = min(md, d[j] + g[j][i]);
					}
				}
				nd[i] = md;
			}
			for(int i = 1; i <= n; i++) {
				d[i] = nd[i];
			}
			// memcpy(d + 1, nd + 1, n);
		}
		ans = min(ans, d[1]);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 100;

int n, k;
int g[N][N];
int startDp[N][N], endDp[N][N];

void solve(int i, int d, int maxD, int start, int totalWeight, int dp[N][N], vector <bool> parity, vector <bool> vis) {
	if(d == maxD) {
		// trace(start, i, totalWeight);
		dp[start][i] = min(dp[start][i], totalWeight);
		return;
	}
	vis[i] = true;
	parity[i] = (d % 2);
	for(int j = 1; j <= n; j++) {
		if(i != j and !(vis[j] and parity[j] == parity[i])) {
			solve(j, d + 1, maxD, start, totalWeight + g[i][j], dp, parity, vis);
		}
	}
}

int main() {
	cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> g[i][j];
			startDp[i][j] = endDp[i][j] = 1e9;
		}
	}

	int mid = k / 2;
	for(int i = 1; i <= n; i++) {
		solve(i, 0, mid, i, 0, startDp, vector <bool>(N), vector <bool>(N));
	}
	for(int i = 1; i <= n; i++) {
		solve(i, 0, k - mid, i, 0, endDp, vector <bool>(N), vector <bool>(N));
	}
	int ans = 1e9;
	for(int i = 1; i <= n; i++) {
		ans = min(ans, startDp[1][i] + endDp[i][1]);
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 100;

int n, k;
int g[N][N];
int startDp[N][N], endDp[N][N];

void solve(int i, int d, int maxD, int start, int totalWeight, int dp[N][N], vector <bool> parity, vector <bool> vis) {
	if(d == maxD) {
		dp[start][i] = min(dp[start][i], totalWeight);
		return;
	}
	vis[i] = true;
	parity[i] = (d % 2);
	for(int j = 1; j <= n; j++) {
		if(i != j and !(vis[j] and parity[j] == parity[i])) {
			solve(j, d + 1, maxD, start, totalWeight + g[i][j], dp, parity, vis);
		}
	}
}

int main() {
	cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> g[i][j];
			startDp[i][j] = endDp[i][j] = 1e9 + 10;
		}
	}

	int mid = k / 2;
	for(int i = 1; i <= n; i++) {
		solve(i, 0, mid, i, 0, startDp, vector <bool>(N, false), vector <bool>(N, false));
	}
	for(int i = 1; i <= n; i++) {
		solve(i, 0, k - mid, i, 0, endDp, vector <bool>(N, false), vector <bool>(N, false));
	}
	int ans = 1e9 + 100;
	for(int i = 1; i <= n; i++) {
		ans = min(ans, startDp[1][i] + endDp[i][1]);
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);

typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 100;

int n, k;
int g[N][N];
int startDp[15][N][N], endDp[15][N][N];

void solve(int i, int d, int maxD, int start, int totalWeight, int dp[15][N][N], vector <bool> parity, vector <bool> vis) {
	dp[d][start][i] = min(dp[d][start][i], totalWeight);
	if(d == maxD) {
		return;
	}
	vis[i] = true;
	parity[i] = (d % 2);
	for(int j = 1; j <= n; j++) {
		if(i != j and !(vis[j] and parity[j] == parity[i])) {
			solve(j, d + 1, maxD, start, totalWeight + g[i][j], dp, parity, vis);
		}
	}
}

int main() {
	cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> g[i][j];
			for(int d = 0; d < 11; d++) {
				startDp[d][i][j] = endDp[d][i][j] = 1e9 + 10;

			}
		}
	}

	int mid = k / 2;
	for(int i = 1; i <= n; i++) {
		solve(i, 0, 5, i, 0, startDp, vector <bool>(N, false), vector <bool>(N, false));
	}
	for(int i = 1; i <= n; i++) {
		solve(i, 0, 5, i, 0, endDp, vector <bool>(N, false), vector <bool>(N, false));
	}
	int ans2 = 1e9 + 100, ans3 = 1e9 + 100, ans4 = 1e9 + 100, ans5 = 1e9 + 100;
	for(int i = 1; i <= n; i++) {
		ans2 = min(ans2, startDp[k / 2][1][i] + endDp[k - k / 2][i][1]);
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			for(int d1 = 1; d1 <= 5; d1++) {
				for(int d2 = 1; d2 <= 5; d2++) {
					if(k - d1 - d2 <= 5 and k - d1 - d2 >= 0) {
						ans3 = min(ans3, startDp[d1][1][i] + startDp[d2][i][j] + startDp[k - d1 - d2][j][1]);
					}
				}
			}
		}
	}
	cout << max(ans2, ans3) << endl;

}