#include #include #include #include struct pt { int x, y; }; int cmp_x(const pt* a, const pt* b) { return a->x - b->x; } int cmp_y(const pt* a, const pt* b) { return a->y - b->y; } struct orchard { orchard(int w, int h, int num_trees) :w(w),h(h),num_trees(num_trees) {} int64_t try_corner(int dx, int dy) { int64_t best_cost = INT64_MAX; int x = (dx > 0) ? trees_by_x[0].x : trees_by_x[num_trees-1].x; int y = (dy > 0) ? trees_by_y[0].y : trees_by_y[num_trees-1].y; int x_index = (dx > 0) ? 0 : (num_trees - 1); for (; x_index < num_trees && x_index >= 0;) { int yy = y; int y_index = (dy > 0) ? 0 : (num_trees - 1); for (; y_index < num_trees && y_index >= 0;) { int xxx = (dx > 0) ? 0 : x; int yyy = (dy > 0) ? 0 : yy; int ww = (dx > 0) ? (x+1) : (w - x - 1); int hh = (dy > 0) ? (yy+1) : (h - yy - 1); if (num_trees_in(xxx, yyy, ww, hh) == num_trees/2) { int64_t cost = (int64_t)ww*hh; if (cost < best_cost) { best_cost = cost; //printf("%ld (%d %d %d %d)\n",cost, xxx,yyy,ww,hh); } } int yy1 = yy; if (dy > 0) { if (y_index + 1 >= num_trees) break; while (yy == yy1 && y_index + 1 <= num_trees) { yy= trees_by_y[++y_index].y; } } else { if (y_index <= 0) break; while (yy == yy1 && y_index >= 0) { yy = trees_by_y[--y_index].y; } } //printf("last y %d next y %d [%d]\n", yy1, yy, y_index); } int x1 = x; if (dx > 0) { if (x_index + 1 >= num_trees) break; while (x == x1 && x_index + 1 < num_trees) { x = trees_by_x[++x_index].x; } } else { if (x_index <= 0) break; while (x == x1 && x_index > 0) { x = trees_by_x[--x_index].x; } } //printf("last x %d next x %d\n", x1, x); } return best_cost; } int num_trees_in(int x, int y, int ww, int hh) { if (x == w-1) x -= ww-1; if (y == h-1) y -= hh-1; int n = 0; for (int i = 0; i < num_trees; i++) { if (trees[i].x >= x && trees[i].x < x + ww && trees[i].y >= y && trees[i].y < y + hh) n++; } return n; } int w,h; pt trees[1000000]; pt trees_by_x[1000000]; pt trees_by_y[1000000]; int num_trees; }; void improve(int64_t& t, int64_t v) { if (v < t) t = v; } int main() { int m, n; while (scanf("%d %d", &m, &n) == 2) { orchard o(m, m, n); for (int i = 0; i < n; i++) { scanf("%d %d", &o.trees[i].x, &o.trees[i].y); } if (n % 2){printf("-1\n"); continue;} memcpy(o.trees_by_x, o.trees, sizeof(o.trees)); memcpy(o.trees_by_y, o.trees, sizeof(o.trees)); qsort(o.trees_by_x, n, sizeof(pt), (int (*)(const void*, const void*))cmp_x); qsort(o.trees_by_y, n, sizeof(pt), (int (*)(const void*, const void*))cmp_y); int64_t c = INT64_MAX; improve(c, o.try_corner(1, 1)); improve(c, o.try_corner(-1, 1)); improve(c, o.try_corner(1, -1)); improve(c, o.try_corner(-1, -1)); if (c == INT64_MAX) c = -1; printf("%ld\n",c); } }