int n, m, k; int a[MAXN][MAXN]; int f[MAXN][MAXN][MAXN][MAXN]; int ans;
intmain(){ memset(f, -1, sizeof f);
scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf("%d", &a[i][j]); f[i][j][0][0] = 0; } }
for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { for (int cnt = 0; cnt <= m / 2; ++cnt) { for (int res = 0; res < k; ++res) { if (f[i][j][cnt][res] == -1) continue;
int ni = (j == m) ? i + 1 : i; int nj = (j == m) ? 1 : j + 1;
if (i != ni) { f[ni][nj][0][res] = max(f[ni][nj][0][res], f[i][j][cnt][res]); } else { f[ni][nj][cnt][res] = max(f[ni][nj][cnt][res], f[i][j][cnt][res]); }
if (cnt <= m / 2 - 1) { int nres = (res + a[i][j]) % k;
while (!st.empty()) { int v = st.begin() -> second; st.erase(st.begin()); for (auto u : g[v]) { int to = u.first; int w = u.second; if (d[to] > d[v] + w) { auto it = st.find({d[to], to}); if (it != st.end()) st.erase(it); d[to] = d[v] + w; st.insert({d[to], to}); } } } }
intmain(){ scanf("%d %d %d", &n, &m, &k); g = vector<vector<pair<int, int> > >(n); for (int i = 0; i < m; ++i) { int x, y, w; scanf("%d %d %d", &x, &y, &w); --x; --y; g[x].push_back({y, w}); g[y].push_back({x, w}); }
d = vector<vector<int> > (n); for (int v = 0; v < n; ++v) { dijkstra(v, d[v]); }
int ans = INF; for (int v = 0; v < n; ++v) { for (auto u : g[v]) { int to = u.first; int w = u.second; int cur = 0; for (auto p : r) { int a = p.first; int b = p.second; cur += min({d[a][b], d[a][v] + d[to][b], d[a][to] + d[v][b]}); } ans = min(ans, cur); } }