#include <bits/stdc++.h>
using namespace std;
int arr[1005][1005] = {0};
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    arr[x][y] = i + 1;
  }
  int mn = 1000000;
  for (int i = 1; i <= n - 2; i++) {
    for (int j = 1; j <= n - 2; j++) {
      int mx = -1;
      bool check = 0;
      for (int k = i; k <= i + 2; k++) {
        for (int l = j; l <= j + 2; l++) {
          if (arr[k][l] == 0) {
            check = 1;
            break;
          }
          mx = max(mx, arr[k][l]);
        }
        if (check) {
          break;
        }
      }
      if (!check) {
        mn = min(mn, mx);
      }
    }
  }
  if (mn == 1000000) {
    cout << -1 << endl;
  } else {
    cout << mn << endl;
  }
  return 0;
}
