#include <bits/stdc++.h>
using namespace std;
const long long N = 1000;
long long n, m, s[N][N];
long long i, j, k;
bool flag = false;
long long step;
void need(long long x, long long y) {
  for (long long i = 1; i <= n; i++) swap(s[i][x], s[i][y]);
  return;
}
void jl(long long x, long long y) {
  long long dif = 0;
  need(x, y);
  for (long long i = 1; i <= n; i++, dif = 0) {
    for (long long j = 1; j <= m; j++) {
      if (s[i][j] != j) dif++;
      if (dif >= 3) {
        need(x, y);
        return;
      }
    }
    if (dif == 1) {
      need(x, y);
      return;
    }
  }
  need(x, y);
  flag = true;
  return;
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) cin >> s[i][j];
  jl(0, 0);
  if (flag == true) {
    cout << "YES" << endl;
    return 0;
  }
  for (i = 1; i <= (m - 1); i++) {
    for (j = i + 1; j <= m; j++) {
      jl(i, j);
      if (flag == true) {
        i = m;
        break;
      }
    }
  }
  if (flag == 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
