#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int mat[N][N], n, m, cnt = 100001;
bool cond(int x, int y, int a, int b) {
  bool flag = true;
  for (int i = a; i < a + 3; i++)
    for (int j = b; j < b + 3; j++)
      if (mat[x + i][y + j] == 0) flag = false;
  if (flag) return true;
  return false;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    mat[x + 2][y + 2] = 1;
    if (cond(x + 2, y + 2, 0, 0)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, 0, -1)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, 0, -2)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, -1, 0)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, -1, -1)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, -1, -2)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, -2, 0)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, -2, -1)) cnt = min(i, cnt);
    if (cond(x + 2, y + 2, -2, -2)) cnt = min(i, cnt);
  }
  if (cnt == 100001)
    cout << -1;
  else
    cout << cnt;
  return 0;
}
