#include <bits/stdc++.h>
void debug_print() {}
template <typename T, typename... Args>
void debug_print(T t, Args... args) {
  std::cout << t << " ";
  debug_print(args...);
}
int findMismatch(int array[25], int M) {
  int count = 0;
  for (int i = 0; i < M; ++i) {
    ;
    ;
    if (array[i] != i) count++;
  };
  ;
  return count;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int matrix[25][25], copyMat[25][25];
  int M, N;
  std::cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      std::cin >> matrix[i][j];
      matrix[i][j]--;
    }
  }
  for (int i = 0; i < N; ++i) {
    int val = findMismatch(matrix[i], M);
    if (i == N - 1 && val <= 2) {
      std::cout << "YES";
      return 0;
    };
    ;
    if (val > 2) break;
  }
  for (int i = 0; i < M; ++i) {
    for (int j = i + 1; j < M; ++j) {
      for (int k = 0; k < N; ++k) {
        std::swap(matrix[k][i], matrix[k][j]);
      };
      ;
      for (int l = 0; l < N; ++l) {
        int val = findMismatch(matrix[l], M);
        if (l == N - 1 && val <= 2) {
          std::cout << "YES";
          return 0;
        };
        ;
        if (val > 2) break;
      }
      for (int k = 0; k < N; ++k) {
        std::swap(matrix[k][i], matrix[k][j]);
      }
    }
  }
  std::cout << "NO";
  return 0;
}
