#include <bits/stdc++.h>
using namespace std;
map<string, long long> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cin >> a[i][j];
  }
  bool f = true;
  for (int k = 0; k < n; k++) {
    int c = 0;
    for (int l = 0; l < m; l++) {
      if (a[k][l] != l + 1) c++;
    }
    if (c > 2) {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "YES\n";
    return 0;
  }
  int b[n][m];
  for (int i = 0; i < m; i++) {
    for (int j = i + 1; j < m; j++) {
      for (int k = 0; k < n; k++) {
        b[k][i] = a[k][j];
        b[k][j] = a[k][i];
      }
      for (int k = 0; k < n; k++) {
        for (int l = 0; l < m; l++) {
          if (l == i || l == j) continue;
          b[k][l] = a[k][l];
        }
      }
      bool f = true;
      for (int k = 0; k < n; k++) {
        int c = 0;
        for (int l = 0; l < m; l++) {
          if (b[k][l] != l + 1) c++;
        }
        if (c > 2) {
          f = false;
          break;
        }
      }
      if (f) {
        cout << "YES\n";
        return 0;
      }
    }
  }
  cout << "NO\n";
  return 0;
}
