#include <bits/stdc++.h>
using namespace std;
int n, m;
int x[32][32];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, to = n; i < to; ++i) {
    for (int j = 0, to = m; j < to; ++j) {
      scanf("%d", &x[i][j]);
    }
  }
  bool can = true;
  for (int i = 0, to = n; i < to; ++i) {
    int r = 0;
    for (int j = 0, to = m; j < to; ++j) {
      r += x[i][j] != j + 1;
    }
    can = can && r <= 2;
  }
  if (can) {
    puts("YES");
    return 0;
  }
  for (int j0 = 0, to = m; j0 < to; ++j0) {
    for (int j1 = j0 + 1, to = m; j1 < to; ++j1) {
      for (int i = 0, to = n; i < to; ++i) swap(x[i][j0], x[i][j1]);
      bool can = true;
      for (int i = 0, to = n; i < to; ++i) {
        int r = 0;
        for (int j = 0, to = m; j < to; ++j) {
          r += x[i][j] != j + 1;
        }
        can = can && r <= 2;
      }
      if (can) {
        puts("YES");
        return 0;
      }
      for (int i = 0, to = n; i < to; ++i) swap(x[i][j0], x[i][j1]);
    }
  }
  puts("NO");
  return 0;
}
