#include <bits/stdc++.h>
using namespace std;
bool p[1000][1000];
int m, n;
bool f(int a, int b);
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    t1--;
    t2--;
    p[t1][t2] = true;
    if (f(t1 - 2, t2 - 2) || f(t1 - 2, t2 - 1) || f(t1 - 2, t2) ||
        f(t1 - 1, t2 - 2) || f(t1 - 1, t2 - 1) || f(t1 - 1, t2) ||
        f(t1, t2 - 2) || f(t1, t2 - 1) || f(t1, t2)) {
      cout << i + 1 << '\n';
      return 0;
    }
  }
  cout << -1 << '\n';
  return 0;
}
bool f(int a, int b) {
  if (a < 0 || b < 0) return false;
  if (a + 2 >= n || b + 2 >= n) return false;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      if (!p[a + i][b + j]) return false;
  return true;
}
