#include using namespace std; #define GET_MACRO(a, b, c, NAME, ...) NAME #define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define rep2(i, a) rep3 (i, 0, a) #define rep3(i, a, b) for (int i = (a); i < (b); i++) #define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__) #define repr2(i, a) repr3 (i, 0, a) #define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--) template inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } using ll = long long; struct BIT { vector dat; BIT(int n) : dat(n) {} void update(int k, long long x) { for (int i = k; i < dat.size(); i += i & -i) dat[i] += x; } long long query(int k) { long long ret = 0; for (int i = k; i > 0; i -= i & -i) ret += dat[i]; return ret; } }; int main() { int n, m; cin >> n >> m; static int A[3][800]; rep(i, m) rep(j, n) cin >> A[i][j]; pair ans; rep(u, m) { ll E = 0; BIT bit(1010); int *a = A[u]; rep(i, n) { BIT bit2(1010); BIT bit3(1010); rep(j, i + 1, n) bit3.update(a[j], 1); rep(j, i + 1, n) { ll cand = 0; if (a[i] == a[j]) continue; // case 1: k < i < j if (a[i] < a[j]) { // a[k] > a[i] and a[k] != a[j] ll val = i - bit.query(a[i]); if (a[j] > a[i]) { val -= bit.query(a[j]) - bit.query(a[j] - 1); } chmax(cand, val); } else { // a[k] < a[i] and a[k] != a[j] ll val = bit.query(a[i] - 1); if (a[j] < a[i]) { val -= bit.query(a[j]) - bit.query(a[j] - 1); } chmax(cand, val); } // case 2: i < k < j // a[k] > max(a[i], a[j]) ll val = (j - i) - bit2.query(max(a[i], a[j])); chmax(cand, val); // case 3: i < j < k if (a[i] < a[j]) { // a[j] > a[k] and a[k] != a[i] ll val = bit3.query(a[j] - 1); if (a[i] < a[j]) { val -= bit3.query(a[i]) - bit3.query(a[i] - 1); } chmax(cand, val); } else { // a[j] < a[k] and a[k] != a[i] ll val = (n - j) - bit3.query(a[j]); if (a[i] > a[j]) { val -= bit3.query(a[i]) - bit3.query(a[i] - 1); } chmax(cand, val); } bit2.update(a[j], 1); bit3.update(a[j], -1); E += cand; } bit.update(a[i], 1); } chmax(ans, make_pair(E, u)); } cout << ans.second << endl; return 0; }