#include <bits/stdc++.h>
using namespace std;
long long read() {
  char c = getchar();
  long long ans = 0;
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) ans = ans * 10 + c - '0', c = getchar();
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n = read();
  long long arr[1005][1005];
  memset(arr, 0, sizeof arr);
  long long moves = read();
  long long dy[9] = {0, 0, 0, -1, -1, -2, -2, -2, -1};
  long long dx[9] = {0, -1, -2, -2, 0, -2, -1, 0, -1};
  auto check = [&](long long i, long long j) {
    if (i < 0 or j < 0 or i + 2 > n or j + 2 > n) return 0;
    if (arr[i + 1][j + 1] and arr[i][j] and arr[i + 1][j] and arr[i + 2][j] and
        arr[i + 2][j + 1] and arr[i + 2][j + 2] and arr[i + 1][j + 2] and
        arr[i][j + 2] and arr[i][j + 1])
      return 1;
    return 0;
  };
  long long f = 0;
  for (long long i = 0; i < moves; i++) {
    long long x = read();
    long long y = read();
    arr[x][y] = 1;
    for (long long j = 0; j < 9; j++) {
      if (check(x + dx[j], y + dy[j])) {
        f = i + 1;
        break;
      }
    }
    if (f) break;
  }
  if (f)
    cout << f;
  else
    cout << "-1";
  return 0;
}
