#include <bits/stdc++.h>
using namespace std;
const int maxn = 25;
int maze[maxn][maxn], another[maxn][maxn];
int a[maxn], b[maxn], n, m;
bool solve(int x, int y) {
  memcpy(another, maze, sizeof(maze));
  for (int i = 1; i <= n; i++) swap(another[i][x], another[i][y]);
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (another[i][j] != j) b[i]++;
  int flag = 1;
  for (int i = 1; i <= n; i++)
    if (b[i] > 2) flag = 0;
  if (flag)
    return true;
  else
    return false;
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(a, 0, sizeof(a));
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        scanf("%d", &maze[i][j]);
        if (maze[i][j] != j) a[i]++;
      }
    int flag = 0, cnt;
    for (int i = 1; i <= n; i++)
      if (a[i] > 2) {
        flag = 1, cnt = i;
        break;
      }
    if (!flag)
      printf("YES\n");
    else {
      if (a[cnt] >= 5)
        printf("NO\n");
      else if (a[cnt] == 3) {
        int x, y, z, num = 0;
        for (int i = 1; i <= m; i++) {
          if (maze[cnt][i] != i) {
            ++num;
            if (num == 1)
              x = i;
            else if (num == 2)
              y = i;
            else if (num == 3)
              z = i;
          }
        }
        if (solve(x, y) || solve(x, z) || solve(y, z))
          printf("YES\n");
        else
          printf("NO\n");
      } else if (a[cnt] == 4) {
        int x, y, z, t, num = 0;
        for (int i = 1; i <= m; i++) {
          if (maze[cnt][i] != i) {
            ++num;
            if (num == 1)
              x = i;
            else if (num == 2)
              y = i;
            else if (num == 3)
              z = i;
            else if (num == 4)
              t = i;
          }
        }
        if (solve(x, y) || solve(x, z) || solve(x, t) || solve(y, z) ||
            solve(y, t) || solve(z, t))
          printf("YES\n");
        else
          printf("NO\n");
      }
    }
  }
  return 0;
}
