#include <bits/stdc++.h>
using namespace std;
int n, m, k, t, c[21][21], vis[21][21], num[4];
struct fuck {
  int cnt, id;
  bool operator<(const fuck& p) const { return cnt > p.cnt; }
};
fuck ca[25];
bool solve(int a, int b) {
  int d[21][21];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) d[i][j] = c[i][j];
  for (int i = 1; i <= n; i++) swap(d[i][a], d[i][b]);
  for (int i = 1; i <= n; i++) {
    int now = 0;
    for (int j = 1; j <= m; j++)
      if (d[i][j] != j) now++;
    if (now > 2) return false;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) ca[i].id = i;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &c[i][j]);
      if (c[i][j] != j) ca[i].cnt++;
    }
  }
  sort(ca + 1, ca + n + 1);
  if (ca[1].cnt <= 2)
    puts("YES");
  else if (ca[1].cnt > 4)
    puts("NO");
  else {
    int cnt = 0;
    int pos = ca[1].id;
    for (int i = 1; i <= m; i++)
      if (c[pos][i] != i) num[cnt++] = i;
    for (int i = 0; i <= 3; i++) {
      for (int j = i + 1; j <= 3; j++) {
        if (solve(num[i], num[j])) return 0 * puts("YES");
      }
    }
    puts("NO");
  }
  return 0;
}
