#include #include #include #include #include #include using namespace std; bool used[801][801]; int main() { int n, m; cin >> n >> m; vector>E(m); for (int i = 0; i < m; i++){ E[i].resize(n); for (int j = 0; j < n; j++){ cin >> E[i][j]; } } vector>A; for (int i = 0; i < m; i++){ for (int s = 0; s < n; s++)for (int t = 0; t < n; t++)used[s][t] = 0; int maxE = n; long long sum = 0; while (E[i].size()){ bool right = false; int Ej = 0; vector>L, R; for (int j = 0; j < E[i].size(); j++){ if (E[i][j] == maxE){ Ej = j; right = true; E[i].erase(E[i].begin() + j); j--; continue; } if (right)R.push_back(make_pair(E[i][j], j - Ej)); else L.push_back(make_pair(E[i][j], j)); } sort(L.begin(), L.end()); sort(R.begin(), R.end()); int cnt = 0; for (int j = 0; j < L.size(); j++){ for (int k = j + 1; k < L.size(); k++){ if (L[k].second < L[j].second){ if (!used[L[j].first][L[k].first])used[L[j].first][L[k].first] = true, cnt++; if (!used[L[j].first][maxE])used[L[j].first][maxE] = true, cnt++; if (!used[L[k].first][maxE])used[L[k].first][maxE] = true, cnt++; } } } for (int j = 0; j < R.size(); j++){ for (int k = j + 1; k < R.size(); k++){ if (R[k].second > R[j].second){ if (!used[R[j].first][R[k].first])used[R[j].first][R[k].first] = true, cnt++; if (!used[R[j].first][maxE])used[R[j].first][maxE] = true, cnt++; if (!used[R[k].first][maxE])used[R[k].first][maxE] = true, cnt++; } } } for (int j = 0; j < R.size(); j++)for (int k = 0; k < L.size(); k++){ if (!used[min(L[k].first, R[j].first)][max(L[k].first, R[j].first)])used[min(L[k].first, R[j].first)][max(L[k].first, R[j].first)] = true, cnt++; } sum += cnt*maxE; maxE--; } A.push_back(make_pair((long long)1e9 - sum, i)); } sort(A.begin(), A.end()); cout << A[0].second << endl; return 0; }