#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int flag = 1;
  scanf("%d %d", &n, &m);
  int a[25][25];
  int b[25][2];
  for (int i = 1; i <= n; i++) {
    int count = 0;
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      if (a[i][j] != j) count++;
    }
    b[i][1] = count;
    if (count > 4) {
      flag = 0;
      printf("NO\n");
      break;
    }
  }
  if (flag == 1) {
    int q;
    int qz = 1;
    for (int i = 1; i <= m; i++) {
      if (flag == 0) break;
      for (int j = 1; j <= m; j++) {
        qz = 1;
        for (int k = 1; k <= n; k++) {
          q = a[k][i];
          a[k][i] = a[k][j];
          a[k][j] = q;
        }
        for (int z = 1; z <= n; z++) {
          int count = 0;
          if (qz == 0) break;
          for (int x = 1; x <= m; x++) {
            if (a[z][x] != x) count++;
            if (count > 2) {
              qz = 0;
              break;
            }
          }
        }
        if (qz == 1) {
          printf("YES\n");
          flag = 0;
          break;
        }
        for (int k = 1; k <= n; k++) {
          q = a[k][i];
          a[k][i] = a[k][j];
          a[k][j] = q;
        }
      }
    }
  }
  if (flag == 1) printf("NO\n");
  return 0;
}
