#include <bits/stdc++.h>
using namespace std;
bool g[1024][1024];
int di[] = {-1, -1, -1, 0, 1, 1, 1, 0, 0};
int dj[] = {-1, 0, 1, 1, 1, 0, -1, -1, 0};
bool debug = false;
bool isSquare(int n, int cx, int cy) {
  if (!(cx > 0 && cx < n - 1 && cy > 0 && cy < n - 1)) {
    return false;
  }
  for (int k = 0; k < 9; k++) {
    int ni = cx + di[k];
    int nj = cy + dj[k];
    if (!g[ni][nj]) {
      return false;
    }
  }
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x][y] = true;
    for (int k = 0; k < 9; k++) {
      int nx = x + di[k];
      int ny = y + dj[k];
      if (isSquare(n, nx, ny)) {
        cout << i + 1 << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
}
