#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
string print_v(vector<T> v) {
  ostringstream oss;
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    oss << *it_i << ", ";
  }
  return oss.str();
}
template <class T>
string print_v(string v) {
  ostringstream oss;
  for (string::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    oss << *it_i << ", ";
  }
  return oss.str();
}
template <class T>
vector<T> input_v() {
  vector<T> res;
  T term;
  while (cin >> term) res.push_back(term);
  return res;
}
template <class T>
vector<T> input_v(int n) {
  vector<T> res(n);
  for (int i = 0; i < n; ++i) cin >> res[i];
  return res;
}
template <class T>
vector<vector<T> > input_vv(int n, int m) {
  vector<vector<T> > res(n, vector<T>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> res[i][j];
    }
  }
  return res;
}
bool valid(int n, int r, int c) {
  if (r < 0 || r >= n) return false;
  if (c < 0 || c >= n) return false;
  return true;
}
bool check2(int n, vector<vector<int> >& M, int r, int c) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (!valid(n, r + i, c + j)) return false;
      if (!M[r + i][c + j]) return false;
    }
  }
  return true;
}
bool check(int n, vector<vector<int> >& M, int r, int c) {
  for (int i = -3; i < 4; ++i) {
    for (int j = -3; j < 4; ++j) {
      if (check2(n, M, r + i, c + j)) return true;
    }
  }
  return false;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    vector<vector<int> > M(n, vector<int>(n, 0));
    int res = INT_MAX;
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      M[y][x] = 1;
      if (check(n, M, y, x)) res = min(res, i + 1);
    }
    cout << (res == INT_MAX ? -1 : res) << endl;
  }
  return 0;
}
