#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int t[20][20];
  int res[21][21][20] = {0};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> t[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    vector<int> wr;
    for (int j = 0; j < m; j++) {
      if (t[i][j] != j + 1) wr.push_back(j + 1);
    }
    if (wr.size() == 0) {
      for (int k = 1; k <= m; k++) {
        for (int l = 1; l <= m; l++) {
          res[k][l][i] = 1;
          res[l][k][i] = 1;
        }
      }
      res[0][0][i] = 1;
    }
    if (wr.size() == 2) {
      res[wr[0]][wr[1]][i] = 1;
      res[wr[1]][wr[0]][i] = 1;
      res[0][0][i] = 1;
    }
    if (wr.size() == 3) {
      res[wr[0]][wr[1]][i] = 1;
      res[wr[1]][wr[0]][i] = 1;
      res[wr[1]][wr[2]][i] = 1;
      res[wr[2]][wr[1]][i] = 1;
      res[wr[0]][wr[2]][i] = 1;
      res[wr[2]][wr[0]][i] = 1;
    }
    if (wr.size() == 4) {
      int a = t[i][wr[0] - 1];
      int b = t[i][a - 1];
      if (b == wr[0]) {
        vector<int> temp;
        for (int k = 1; k <= 3; k++) {
          if (wr[k] != a) temp.push_back(wr[k]);
        }
        res[a][b][i] = 1;
        res[b][a][i] = 1;
        res[temp[0]][temp[1]][i] = 1;
        res[temp[1]][temp[0]][i] = 1;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    }
    if (wr.size() > 4) {
      cout << "NO" << endl;
      return 0;
    }
  }
  bool flag = true;
  for (int i = 0; i < n; i++) {
    if (res[0][0][i] != 1) flag = false;
  }
  if (flag) {
    cout << "YES" << endl;
    return 0;
  }
  for (int i = 1; i <= m; i++) {
    for (int j = i + 1; j <= m; j++) {
      flag = true;
      for (int k = 0; k < n; k++) {
        if (res[i][j][k] != 1) flag = false;
      }
      if (flag) {
        cout << "YES" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
  return 0;
}
