import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum N = 5; enum V = N * N; int[] G, GSum; bool[] vis; void dfs(int p) { if (!vis[p]) { vis[p] = true; if (2 * popcnt(p) < V) { foreach (v; 0 .. V) { if ((GSum[p] >> v) & 1) { dfs(p | 1 << v); } } } } } enum INF = 10L^^18; void main() { G = new int[V]; foreach (u; 0 .. V) foreach (v; 0 .. V) { if (abs(u / N - v / N) + abs(u % N - v % N) == 1) { G[u] |= 1 << v; } } GSum = new int[1 << V]; foreach (u; 0 .. V) foreach (p; 0 .. 1 << u) { GSum[p | 1 << u] = GSum[p] | G[u]; } vis = new bool[1 << V]; foreach (u; 0 .. V) { dfs(1 << u); } stderr.writeln(vis.count(true)); try { for (; ; ) { auto C = new long[V]; foreach (u; 0 .. V) { C[u] = readLong(); } long ans = INF; foreach (p; 0 .. 1 << V) { if (vis[p]) { long diff; foreach (u; 0 .. V) { diff += (((p >> u) & 1) ? -1 : +1) * C[u]; } chmin(ans, abs(diff)); } } writeln(ans); } } catch (EOFException e) { } }