#include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef long double ld; typedef pair ii; typedef vector vi; typedef vector vvi; typedef vector vii; #define x first #define y second #define pb push_back #define eb emplace_back #define rep(i,a,b) for(auto i=(a); i<(b); ++i) #define REP(i,n) rep(i,0,n) #define all(v) (v).begin(), (v).end() #define rs resize #define DBG(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl const ld PI = acos(-1.0); template using min_queue = priority_queue, greater>; template int sz(const T &x) { return (int) x.size(); // copy the ampersand(&)! } template ostream& operator<<(ostream &os, vector &v) { os << "\n["; for(T &x : v) os << x << ','; return os << "]\n"; } struct pairhash { public: template size_t operator()(const pair &p) const { size_t lhs = hash()(p.x); size_t rhs = hash()(p.y); return lhs ^ (rhs+0x9e3779b9+(lhs<<6)+(lhs>>2)); } }; void run() { ll n, k; cin >> n >> k; vvi I(n); for (ll i = 0; i < k; i++) { for (ll j = 0; j < n; j++) { ll s; cin >> s; I[s-1].pb(j); } } vi R(n); for (ll i = 0; i < n; i++) R[i] = i; ll i = 0; while (i+1 < n) { ll oldcost = 0, newcost = 0; for (ll j : I[R[i]]) { oldcost += (j-i)*(j-i); newcost += (j-i-1)*(j-i-1); } for (ll j : I[R[i+1]]) { oldcost += (j-i-1)*(j-i-1); newcost += (j-i)*(j-i); } if (newcost < oldcost) { swap(R[i], R[i+1]); if (i > 0) i--; } else { i++; } } for (ll r : R) { cout << r+1 << " "; } cout << endl; } signed main() { // DON'T MIX "scanf" and "cin"! ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(20); run(); return 0; }