#include <bits/stdc++.h>
int n, m, map[25][25];
void swap(int &x, int &y) {
  int a = x;
  x = y;
  y = a;
}
bool check() {
  for (int i = 1; i <= n; i++) {
    int cnt = 0, pla[50];
    for (int k = 1; k <= m; k++) {
      if (map[i][k] != k) pla[++cnt] = k;
    }
    if (cnt > 2) return false;
    if (cnt == 0) continue;
    if (map[i][pla[1]] != pla[2]) return false;
    if (map[i][pla[2]] != pla[1]) return false;
  }
  return true;
}
int main() {
  int i, k, j, t, ans = 1;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    for (k = 1; k <= m; k++) {
      scanf("%d", &map[i][k]);
      if (map[i][k] != k) ans = 0;
    }
  }
  if (check()) ans = 1;
  if (!ans) {
    for (i = 1; i <= m && !ans; i++) {
      for (k = i + 1; k <= m && !ans; k++) {
        for (j = 1; j <= n; j++) swap(map[j][i], map[j][k]);
        if (check()) ans = 1;
        for (j = 1; j <= n; j++) swap(map[j][i], map[j][k]);
      }
    }
  }
  if (ans)
    printf("YES");
  else
    printf("NO");
  return 0;
}
