#include <bits/stdc++.h>
using namespace std;
/*
PROBLEM (simple)
----------------
There are n locations. Each location initially has resources.
Moving ALL resources from i -> j costs cost[i][j].
You are allowed to move via intermediate locations (i -> x -> j) if that is cheaper.
Goal:
After all moves, resources should remain in AT MOST k locations (you choose which k hubs).
Every other location must send its resources to one of the chosen hubs.
Minimize total cost.
Key observation
---------------
1) Since routing via intermediates is allowed, first compute the cheapest cost between every pair:
-> Floyd–Warshall gives dist[i][j] = minimum shipping cost from i to j.
2) If we choose a set of hubs S (|S| <= k), then each location i will ship to the cheapest hub:
cost for i = min_{h in S} dist[i][h]
Total cost for hub set S:
sum_i min_{h in S} dist[i][h]
3) For small n (<= ~20–22), we can try all hub sets using a bitmask:
mask bit h = 1 => hub h is kept
For each mask, compute total cost efficiently in O(n * 2^n):
For each location i:
bestToSet[mask] = min_{h in mask} dist[i][h]
Then:
totalCost[mask] = sum over i of bestToSet[mask]
Answer = min totalCost[mask] for popcount(mask) <= k
Complexity
----------
Floyd–Warshall: O(n^3)
Bitmask DP: O(n * 2^n)
Memory: O(n^2) + O(2^n)
NOTE:
- This exact approach is practical only for n <= 20–22.
- If your input uses -1 for "no direct path", convert -1 to INF while reading.
*/
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
const long long INF = (1LL << 62);
// Exact bitmask DP becomes infeasible beyond ~22
if (n > 22) {
cout << -1 << "\n";
return 0;
}
if (k == 0) { // usually not present in OAs, but safe
cout << -1 << "\n";
return 0;
}
// Read cost matrix
vector<vector<long long>> dist(n, vector<long long>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
long long x;
cin >> x;
// If your problem says "-1 means no path", use:
// dist[i][j] = (x == -1 ? INF : x);
dist[i][j] = x;
}
}
// Keeping location i means no move => 0 cost on diagonal
for (int i = 0; i < n; i++) dist[i][i] = 0;
// Floyd–Warshall: compute shortest shipping costs (allow intermediates)
for (int mid = 0; mid < n; mid++) {
for (int i = 0; i < n; i++) {
if (dist[i][mid] >= INF / 2) continue; // i->mid unreachable
for (int j = 0; j < n; j++) {
if (dist[mid][j] >= INF / 2) continue; // mid->j unreachable
long long cand = dist[i][mid] + dist[mid][j];
if (cand < dist[i][j]) dist[i][j] = cand;
}
}
}
// If we can keep all locations, no moves needed
if (k >= n) {
cout << 0 << "\n";
return 0;
}
int ALL = 1 << n;
// totalCost[mask] = total cost if we keep exactly the hub set represented by mask
vector<long long> totalCost(ALL, 0);
// bestToSet[mask] (reused per location i):
// bestToSet[mask] = min_{h in mask} dist[i][h]
vector<long long> bestToSet(ALL);
// Build totalCost by processing each location one at a time
for (int i = 0; i < n; i++) {
bestToSet[0] = INF; // empty hub set is invalid
// DP over masks:
// bestToSet[mask] = min(bestToSet[mask without lowest bit], dist[i][hub(lowest bit)])
for (int mask = 1; mask < ALL; mask++) {
int hub = __builtin_ctz((unsigned)mask); // index of lowest set bit
int prev = mask & (mask - 1); // remove that bit
bestToSet[mask] = min(bestToSet[prev], dist[i][hub]);
}
// Add this location's contribution into totalCost (skip mask=0)
for (int mask = 1; mask < ALL; mask++) {
__int128 sum = (__int128)totalCost[mask] + bestToSet[mask];
totalCost[mask] = (sum > (__int128)INF) ? INF : (long long)sum;
}
}
// Answer = best totalCost[mask] among masks with <= k hubs
long long ans = INF;
for (int mask = 1; mask < ALL; mask++) {
if (__builtin_popcount((unsigned)mask) <= k) {
ans = min(ans, totalCost[mask]);
}
}
// If unreachable paths exist, ans may remain INF
if (ans >= INF / 2) cout << -1 << "\n";
else cout << ans << "\n";
return 0;
}