#include <bits/stdc++.h>
using namespace std;
int used[1024][1024];
int main() {
  int n, m, i;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int pat = i;
    int x, y;
    cin >> x >> y;
    x++;
    y++;
    used[x][y] = 1;
    for (int i = x - 2; i <= x; i++) {
      for (int j = y - 2; j <= y; j++) {
        int sum = 0;
        for (int i2 = i; i2 < i + 3; i2++) {
          for (int j2 = j; j2 < j + 3; j2++) {
            sum += used[i2][j2];
          }
        }
        if (sum == 9) {
          cout << pat + 1 << endl;
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
