#include <bits/stdc++.h>
using namespace std;
/*
PROBLEM (in simple words)
-------------------------
There are n locations. Moving all resources from i -> j costs cost[i][j].
You are allowed to move via intermediate locations (i -> x -> j) if cheaper.
Goal: After moving, resources should remain in AT MOST k locations (you choose which).
Every other location sends its resources to one of the chosen "hub" locations.
Minimize total cost.
Key ideas
---------
1) Floyd–Warshall:
Because intermediate routing is allowed, first convert cost[][] into shortest-path costs.
2) Choose hubs (<= k):
If we choose a hub set S, then each location i pays:
min_{h in S} dist[i][h]
Total cost = sum over all i of that minimum.
3) Exact solution for small n (n <= ~20-22):
Enumerate all hub sets using bitmasks, but efficiently using DP:
For each location i, compute bestToSet[mask] = min cost from i to any hub in 'mask'.
Then accumulate into totalCost[mask] += bestToSet[mask].
Complexities
------------
- Floyd–Warshall: O(n^3)
- Bitmask DP: O(n * 2^n)
- Memory: O(2^n)
NOTE:
- This exact approach is feasible only when n is small (typically <= 22).
- If your OA guarantees small n, you can keep the guard.
*/
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
const long long INF = (1LL << 62);
// If n is too large, exact bitmask DP is not feasible.
// If your OA guarantees n <= 20/22, this guard is fine.
if (n > 22) {
cout << -1 << "\n";
return 0;
}
// If k == 0, you cannot keep resources anywhere (usually not asked, but safe).
if (k == 0) {
cout << -1 << "\n";
return 0;
}
// Read cost matrix.
// If your problem uses -1 meaning "no direct transfer", convert it to INF.
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;
// Common OA convention:
// if (x == -1) dist[i][j] = INF; else dist[i][j] = x;
dist[i][j] = x;
}
}
// Keeping a location means no move -> cost must be 0 on diagonal.
for (int i = 0; i < n; i++) dist[i][i] = 0;
// Floyd–Warshall: compute shortest shipping cost between every pair.
for (int mid = 0; mid < n; mid++) {
for (int i = 0; i < n; i++) {
if (dist[i][mid] >= INF / 2) continue; // no point continuing if i->mid is impossible
for (int j = 0; j < n; j++) {
if (dist[mid][j] >= INF / 2) continue; // mid->j impossible
long long cand = dist[i][mid] + dist[mid][j];
if (cand < dist[i][j]) dist[i][j] = cand;
}
}
}
// If we can keep all locations, we move nothing.
if (k >= n) {
cout << 0 << "\n";
return 0;
}
int ALL = 1 << n; // number of subsets
// totalCost[mask] = total cost if we keep exactly the hub set 'mask'
vector<long long> totalCost(ALL, 0);
// bestToSet[mask] will be reused for each location i:
// bestToSet[mask] = min_{h in mask} dist[i][h]
vector<long long> bestToSet(ALL);
// Build totalCost by processing each location one-by-one.
for (int i = 0; i < n; i++) {
// Empty hub set is invalid; set its cost to INF.
bestToSet[0] = INF;
// DP over masks:
// bestToSet[mask] = min(bestToSet[mask without lowest bit], dist[i][lowestBitHub])
for (int mask = 1; mask < ALL; mask++) {
int hub = __builtin_ctz((unsigned)mask); // index of lowest set bit
int prev = mask & (mask - 1); // mask with that bit removed
bestToSet[mask] = min(bestToSet[prev], dist[i][hub]);
}
// Add this location's cost to each hub set (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 = minimum totalCost[mask] among masks with popcount(mask) <= k
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 and no valid hub set works, ans may remain INF.
if (ans >= INF / 2) cout << -1 << "\n";
else cout << ans << "\n";
return 0;
}