#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; constexpr ll INF = 1ll << 60; template class SegmentTree { private: using F = function; // モノイド型 int n; // 横幅 F f; // モノイド T e; // モノイド単位元 vector data; public: // init忘れに注意 SegmentTree() {} SegmentTree(F f, T e) :f(f), e(e) {} void init(int n_) { n = 1; while (n < n_)n <<= 1; data.assign(n << 1, e); } void build(const vector& v) { int n_ = v.size(); init(n_); rep(i, n_)data[n + i] = v[i]; for (int i = n - 1; i >= 0; i--) { data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]); } } void set_val(int idx, T val) { idx += n; data[idx] = val; while (idx >>= 1) { data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]); } } T query(int a, int b) { // [a,b) T vl = e, vr = e; for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1)vl = f(vl, data[l++]); // unknown if (r & 1)vr = f(data[--r], vr); // unknown } return f(vl, vr); } }; ll estimate(vector& e) { // seg init const int N = e.size(); auto f1 = [](ll a, ll b) {return max(a, b); }; auto f2 = [](ll a, ll b) {return min(a, b); }; SegmentTree seg_max(f1, -INF), seg_min(f2, INF); seg_max.build(e), seg_min.build(e); // map init map mp; rep(i, N)mp[e[i]] = i; // query ll res = 0; REP(b, 1, N + 1)REP(a, 1, b) { ll l = mp[a], r = mp[b]; ll tres = 0; if (l < r) { ll big = seg_max.query(0, r); if (big > b)tres = big; else { if (seg_max.query(0, l) > a || seg_min.query(r, N) < b || seg_min.query(l, N) < a) tres = b; } } else { swap(l, r); ll big = seg_max.query(l, N); if (big > b)tres = big; else { if (seg_max.query(r, N) > a || seg_min.query(0, l) < b || seg_min.query(0, r) < a) tres = b; } } res += tres; } return res; } int main() { // input int N, M; cin >> N >> M; vector> E(M, vector(N)); rep(i, M)rep(j, N)cin >> E[i][j]; vector> score; rep(i, M) { score.push_back({ estimate(E[i]),-i }); } sort(score.begin(), score.end()); cout << -score.back().second << endl; return 0; }