#include <bits/stdc++.h>
using namespace std;
int A[24][24], B[24][24];
int cnt[25], pos[25];
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) scanf("%d", &A[i][j]);
    for (int j = 1; j <= M; ++j) cnt[i] += (A[i][j] != j);
    if (cnt[i] > 4) {
      puts("NO");
      return 0;
    }
  }
  bool ok = true;
  for (int i = 1; i <= N; ++i)
    if (cnt[i] != 2 && cnt[i] != 0) ok = false;
  if (ok) {
    puts("YES");
    return 0;
  }
  for (int j = 1; j <= M; ++j) {
    for (int k = j + 1; k <= M; ++k) {
      bool ok = true;
      for (int i = 1; i <= N; ++i) {
        if (A[i][j] != k && A[i][k] != j) {
          if (A[i][j] == j && A[i][k] == k) {
            if (cnt[i] == 0)
              continue;
            else {
              ok = false;
              break;
            }
          } else {
            ok = false;
            break;
          }
        } else if (A[i][j] != k) {
          if (k != A[i][A[i][j]]) {
            ok = false;
            break;
          }
        } else if (A[i][k] != j) {
          if (j != A[i][A[i][k]]) {
            ok = false;
            break;
          }
        }
      }
      if (ok) {
        puts("YES");
        return 0;
      }
    }
  }
  puts("NO");
  return 0;
}
