#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a[50], b[50], c[50], d[50];
  cin >> n >> m;
  for (int i = 0; i < 50; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 0; i < n; i++) {
    int ta =
        (max(a[i], c[0]) - min(a[i], c[0]) + max(b[i], d[0]) - min(b[i], d[0]));
    for (int j = 0; j < m; j++) {
      if (ta > (max(a[i], c[i]) - min(a[i], c[i]) + max(b[i], d[i]) -
                min(b[i], d[i]))) {
        ta = (max(a[i], c[i]) - min(a[i], c[i]) + max(b[i], d[i]) -
              min(b[i], d[i]));
      }
    }
    cout << ta << endl;
  }
  return 0;
}
