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)); } int N, M; int[][] E; void main() { try { for (; ; ) { N = readInt(); M = readInt(); E = new int[][](M, N); foreach (m; 0 .. M) { foreach (i; 0 .. N) { E[m][i] = readInt(); } } int maxSum = -1; int ans = -1; foreach (m; 0 .. M) { auto mn = new int[][](N + 1, N + 1); auto mx = new int[][](N + 1, N + 1); foreach (i; 0 .. N + 1) { mn[i][] = N + 1; mx[i][] = 0; } foreach (i; 0 .. N + 1) { foreach (j; i .. N) { mn[i][j + 1] = min(mn[i][j], E[m][j]); mx[i][j + 1] = max(mx[i][j], E[m][j]); } } int sum; foreach (i; 0 .. N) foreach (j; i + 1 .. N) { int win; if (E[m][i] < E[m][j]) { // x i E[m][i]) { chmax(win, max(mx[0][i], E[m][j])); } // i x j if (mn[i + 1][j] < E[m][i]) { chmax(win, E[m][j]); } if (mx[i + 1][j] > E[m][j]) { chmax(win, mx[i + 1][j]); } // ij if (mn[0][i] < E[m][i]) { chmax(win, E[m][i]); } // i x j if (mn[i + 1][j] < E[m][j]) { chmax(win, E[m][i]); } if (mx[i + 1][j] > E[m][i]) { chmax(win, mx[i + 1][j]); } // i>j x if (mx[j + 1][N] > E[m][j]) { chmax(win, max(mx[j + 1][N], E[m][i])); } } debug { writeln(m, " ", E[m][i], " ", E[m][j], ": ", win); } sum += win; } debug { writeln(m, ": ", sum); } if (chmax(maxSum, sum)) { ans = m; } } writeln(ans); } } catch (EOFException e) { } }