#include <stdio.h>
#include <time.h>
// #define FILE_TEST
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define INF 13
int T, n, n_sqr;
int a[110][110];
int idx(int i, int j, int k) { return (i - 1) * n_sqr + (j - 1) * n + (k - 1); }
int real_idx(int _i, int _j, int _k)
{
int i = min(_i, min(_j, _k)), k = max(_i, max(_j, _k)), j = _i ^ _j ^ _k ^ i ^ k;
return (i - 1) * n_sqr + (j - 1) * n + (k - 1);
}
int cnt(int i, int j, int k) { return a[i][j] + a[j][k] + a[i][k]; }
int vec[1000010], pos[1000010], sz;
void _insert(int x) { vec[++sz] = x, pos[x] = sz; }
void _remove(int x) { pos[vec[sz]] = pos[x], vec[pos[x]] = vec[sz], vec[sz--] = 0; }
char empty() { return !sz; }
void clear()
{
for (int i = 1; i <= sz; ++i)
pos[vec[i]] = 0, vec[i] = 0;
sz = 0;
}
int global_ans;
void change(int _i, int _j)
{
int off = (a[_i][_j] == 1) ? 1 : -1;
for (int _k = 1; _k <= n; ++_k)
if ((_k ^ _i) && (_k ^ _j) && cnt(_i, _j, _k) == 2)
_remove(real_idx(_i, _j, _k));
a[_i][_j] = a[_j][_i] = a[_i][_j] ^ 1;
for (int _k = 1; _k <= n; ++_k)
if ((_k ^ _i) && (_k ^ _j) && cnt(_i, _j, _k) == 2)
_insert(real_idx(_i, _j, _k));
}
void dfs(int cur)
{
if (empty())
{
global_ans = cur - 1;
return;
}
if (cur >= global_ans)
return;
int sta = vec[1];
int i = sta / n_sqr + 1, j = ((sta / n) % n) + 1, k = sta % n + 1;
change(i, j), dfs(cur + 1), change(i, j);
change(j, k), dfs(cur + 1), change(j, k);
change(i, k), dfs(cur + 1), change(i, k);
}
int main()
{
#ifdef FILE_TEST
clock_t start, finish;
#endif
for (int t = 1; t <= T; ++t)
{
clear();
scanf("%d", &n
), n_sqr
= n
* n
; global_ans = INF;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
for (int k = j + 1; k <= n; ++k)
if (cnt(i, j, k) == 2)
_insert(idx(i, j, k));
dfs(1);
printf("%d\n", global_ans
== INF
? -1 : global_ans
); // printf("Case #%d: %d\n", t, global_ans == INF ? -1 : global_ans);
}
#ifdef FILE_TEST
fprintf(stderr
, "time : %.3f s\n", ((double)(finish
- start
)) / CLOCKS_PER_SEC
); #endif
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx0aW1lLmg+Ci8vICNkZWZpbmUgRklMRV9URVNUCiNkZWZpbmUgbWF4KGEsIGIpICgoKGEpID4gKGIpKSA/IChhKSA6IChiKSkKI2RlZmluZSBtaW4oYSwgYikgKCgoYSkgPCAoYikpID8gKGEpIDogKGIpKQojZGVmaW5lIElORiAxMwppbnQgVCwgbiwgbl9zcXI7CmludCBhWzExMF1bMTEwXTsKaW50IGlkeChpbnQgaSwgaW50IGosIGludCBrKSB7IHJldHVybiAoaSAtIDEpICogbl9zcXIgKyAoaiAtIDEpICogbiArIChrIC0gMSk7IH0KaW50IHJlYWxfaWR4KGludCBfaSwgaW50IF9qLCBpbnQgX2spIAp7CiAgICBpbnQgaSA9IG1pbihfaSwgbWluKF9qLCBfaykpLCBrID0gbWF4KF9pLCBtYXgoX2osIF9rKSksIGogPSBfaSBeIF9qIF4gX2sgXiBpIF4gazsgCiAgICByZXR1cm4gKGkgLSAxKSAqIG5fc3FyICsgKGogLSAxKSAqIG4gKyAoayAtIDEpOyAKfQppbnQgY250KGludCBpLCBpbnQgaiwgaW50IGspIHsgcmV0dXJuIGFbaV1bal0gKyBhW2pdW2tdICsgYVtpXVtrXTsgfQppbnQgdmVjWzEwMDAwMTBdLCBwb3NbMTAwMDAxMF0sIHN6Owp2b2lkIF9pbnNlcnQoaW50IHgpIHsgdmVjWysrc3pdID0geCwgcG9zW3hdID0gc3o7IH0Kdm9pZCBfcmVtb3ZlKGludCB4KSB7IHBvc1t2ZWNbc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2UoaSwgaiksIGRmcyhjdXIgKyAxKSwgY2hhbmdlKGksIGopOwogICAgY2hhbmdlKGosIGspLCBkZnMoY3VyICsgMSksIGNoYW5nZShqLCBrKTsKICAgIGNoYW5nZShpLCBrKSwgZGZzKGN1ciArIDEpLCBjaGFuZ2UoaSwgayk7Cn0KCmludCBtYWluKCkKewojaWZkZWYgRklMRV9URVNUICAKICAgIGZyZW9wZW4oImluLnR4dCIsICJyIiwgc3RkaW4pOwogICAgY2xvY2tfdCBzdGFydCwgZmluaXNoOwogICAgc3RhcnQgPSBjbG9jaygpOwojZW5kaWYKICAgIHNjYW5mKCIlZCIsICZUKTsKICAgIGZvciAoaW50IHQgPSAxOyB0IDw9IFQ7ICsrdCkKICAgIHsKICAgICAgICBjbGVhcigpOwogICAgICAgIHNjYW5mKCIlZCIsICZuKSwgbl9zcXIgPSBuICogbjsKICAgICAgICBnbG9iYWxfYW5zID0gSU5GOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbjsgKytqKQogICAgICAgICAgICAgICAgc2NhbmYoIiVkIiwgJmFbaV1bal0pOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkKICAgICAgICAgICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDw9IG47ICsraikKICAgICAgICAgICAgICAgIGZvciAoaW50IGsgPSBqICsgMTsgayA8PSBuOyArK2spCiAgICAgICAgICAgICAgICAgICAgaWYgKGNudChpLCBqLCBrKSA9PSAyKQogICAgICAgICAgICAgICAgICAgICAgICBfaW5zZXJ0KGlkeChpLCBqLCBrKSk7CgogICAgICAgIGRmcygxKTsKICAgICAgICBwcmludGYoIiVkXG4iLCBnbG9iYWxfYW5zID09IElORiA/IC0xIDogZ2xvYmFsX2Fucyk7CiAgICAgICAgLy8gcHJpbnRmKCJDYXNlICMlZDogJWRcbiIsIHQsIGdsb2JhbF9hbnMgPT0gSU5GID8gLTEgOiBnbG9iYWxfYW5zKTsKICAgIH0KI2lmZGVmIEZJTEVfVEVTVCAgIAogICAgZmluaXNoID0gY2xvY2soKTsKICAgIGZwcmludGYoc3RkZXJyLCAidGltZSA6ICUuM2Ygc1xuIiwgKChkb3VibGUpKGZpbmlzaCAtIHN0YXJ0KSkgLyBDTE9DS1NfUEVSX1NFQyk7CiNlbmRpZgp9