#include <bits/stdc++.h>
using namespace std;
int N;
int grid[1010][1010];
bool square(int i, int j) {
  return grid[i - 1][j - 1] && grid[i - 1][j] && grid[i - 1][j + 1] &&
         grid[i][j - 1] & grid[i][j] && grid[i][j + 1] &&
         grid[i + 1][j - 1] & grid[i + 1][j] && grid[i + 1][j + 1];
}
bool verify(int i, int j) {
  for (int k = -1; k <= 1; k++) {
    for (int l = -1; l <= 1; l++) {
      if (i + k - 1 >= 0 && i + k + 1 < N && j + l - 1 >= 0 && j + l + 1 < N) {
        if (square(i + k, j + l)) {
          return true;
        }
      }
    }
  }
  return false;
}
int main() {
  int M;
  int i, j;
  scanf("%d %d", &N, &M);
  for (int k = 1; k <= M; k++) {
    scanf("%d %d", &i, &j);
    grid[i - 1][j - 1] = 1;
    if (verify(i - 1, j - 1)) {
      printf("%d\n", k);
      return EXIT_SUCCESS;
    }
  }
  puts("-1");
  return EXIT_SUCCESS;
}
