#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define REP0(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define printclock cerr << "\nTime : " << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms\n";
const int mod = 1e9 + 7, inf = 1000111000, mxr = 305, mxn = 2e6 + 5;
int adj[(int)3e5 + 5][7];
int opp[7], n, r;
bool dd[mxn];
long long ans = 0;
vector<int> bo;
namespace sub125
{
void solve()
{
for (int i = 0; i <= 3 * r * (r + 1); i++)
{
vector<int> vt(7, i);
while (1)
{
int e = 1;
REP(j, 6)
{
vt[j] = adj[vt[j]][j];
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
if (dd[vt[j]])
e = 0;
}
if (e == -1)
break;
else
{
if (e == 1)
ans++;
int se = vt[2];
vector<int> vt2 = vt;
while (1)
{
int e = 1;
REP(j, 6)
{
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt2[j] = adj[vt2[j]][angle];
if (j == 1 && vt2[j] == se)
{
e = -1;
break;
}
if (vt2[j] == -1 || vt2[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
if (dd[vt2[j]])
e = 0;
}
if (e == -1)
break;
else if (e == 1)
ans++;
}
}
}
}
cout << ans << '\n';
}
}
namespace sub6
{
int adj2[(int)3e5 + 5][12][7], huong[305][7];
vector<int> popbit[1005];
inline int get(int x, int k, int d)
{
// cerr << x << ' ' << k << ' ' << d << '\n';
if (x == -1)
return x;
for (auto i : popbit[k])
{
if (x == -1)
return x;
x = adj2[x][i][d];
}
return x;
}
void solve()
{
REP(i, r)
{
ans += i;
}
ans *= ans;
// cbi truoc mang adj2
memset(adj2, -1, sizeof adj2);
for (int i = 0; i <= 3 * r * (r + 1); i++)
{
REP(j, 6)
adj2[i][0][j] = adj[i][j];
}
for (int k = 1; (1 << k) <= 1000; k++)
{
REP(j, 6)
{
for (int i = 0; i <= 3 * r * (r + 1); i++)
{
if (adj2[i][k - 1][j] == -1)
{
adj2[i][k][j] = -1;
continue;
}
adj2[i][k][j] = adj2[adj2[i][k - 1][j]][k - 1][j];
}
}
}
REP(i, 1000)
{
for (int j = 0; (1 << j) <= i; j++)
{
if (i >> j & 1)
popbit[i].pb(j);
}
}
// solve
vector<int> vt(7);
for (int i : bo)
{
FOR(h, 0, r)
{
REP(d, 6)
{
huong[h][d] = get(i, h, d);
}
}
REP(m, r)
{
REP0(h, m)
{
// A
int start = huong[h][6];
int tam = get(start, m, 4);
int e = 1;
REP(j, 6)
{
vt[j] = get(tam, m, j);
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt[j] = get(vt[j], h, angle);
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
}
if (e == 1 && vt[1] == i)
ans--;
// B
start = huong[h][1];
tam = get(start, m, 5);
e = 1;
REP(j, 6)
{
vt[j] = get(tam, m, j);
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt[j] = get(vt[j], h, angle);
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
}
if (e == 1 && !dd[vt[1]] && vt[2] == i)
ans--;
// C
start = huong[h][2];
tam = get(start, m, 6);
e = 1;
REP(j, 6)
{
vt[j] = get(tam, m, j);
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt[j] = get(vt[j], h, angle);
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
}
if (e == 1 && !dd[vt[1]] && !dd[vt[2]] && vt[3] == i)
ans--;
// D
start = huong[h][3];
tam = get(start, m, 1);
e = 1;
REP(j, 6)
{
vt[j] = get(tam, m, j);
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt[j] = get(vt[j], h, angle);
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
}
if (e == 1 && !dd[vt[1]] && !dd[vt[2]] && !dd[vt[3]] && vt[4] == i)
ans--;
// E
start = huong[h][4];
tam = get(start, m, 2);
e = 1;
REP(j, 6)
{
vt[j] = get(tam, m, j);
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt[j] = get(vt[j], h, angle);
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
}
if (e == 1 && !dd[vt[1]] && !dd[vt[2]] && !dd[vt[3]] && !dd[vt[4]] && vt[5] == i)
ans--;
// F
start = huong[h][5];
tam = get(start, m, 3);
e = 1;
REP(j, 6)
{
vt[j] = get(tam, m, j);
int angle = (j + 2) % 6;
if (angle == 0)
angle = 6;
vt[j] = get(vt[j], h, angle);
if (vt[j] == -1 || vt[j] > 3 * (r) * (r + 1))
{
e = -1;
break;
}
}
if (e == 1 && !dd[vt[1]] && !dd[vt[2]] && !dd[vt[3]] && !dd[vt[4]] && !dd[vt[5]] && vt[6] == i)
ans--;
}
}
}
cout << ans << '\n';
}
}
int32_t main()
{
#define task ""
if (fopen(task ".inp", "r"))
{
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> r >> n;
if (n == 0)
{
int ans = 0;
REP(i, r)
{
ans += i;
}
cout << ans * ans << '\n';
return 0;
}
opp[1] = 4;
opp[2] = 5;
opp[3] = 6;
opp[4] = 1;
opp[5] = 2;
opp[6] = 3;
memset(adj, -1, sizeof adj);
REP(i, n)
{
int x;
cin >> x;
dd[x] = 1;
bo.pb(x);
}
REP(i, 6)
{
adj[0][i] = i;
adj[i][opp[i]] = 0;
}
int s = 2;
REP(i, 6)
{
int numitr = (i + 1) % 6;
if (numitr == 0)
numitr = 6;
s = (s + 1) % 6;
if (s == 0)
s = 6;
adj[i][s] = numitr;
adj[numitr][opp[s]] = i;
}
REP(i, r)
{
int x = 3 * (i + 2) * (i + 1), angle = 6, same = 2;
for (int itr = 3 * i * (i - 1) + 1, cnt = 0; itr <= 3 * (i + 1) * i; itr++, cnt = (cnt + 1) % i)
{
int num = (cnt == 0) ? 3 : 2;
if (cnt == 0)
{
same = (same + 1) % 6;
if (same == 0)
same = 6;
}
int numadj = itr + 1;
if (numadj == 3 * (i + 1) * i + 1)
numadj = 3 * i * (i - 1) + 1;
adj[itr][same] = numadj;
adj[numadj][opp[same]] = itr;
int x2 = x, angle2 = angle;
REP(j, num)
{
adj[itr][angle2] = x2;
adj[x2][opp[angle2]] = itr;
angle2 = (angle2 + 1) % 6;
if (angle2 == 0)
angle2 = 6;
x2++;
if (x2 == 3 * (i + 2) * (i + 1) + 1)
x2 = 3 * (i + 1) * i + 1;
}
REP(j, num - 1)
{
x++;
if (x == 3 * (i + 2) * (i + 1) + 1)
x = 3 * (i + 1) * i + 1;
}
REP(j, num - 2)
{
angle = (angle + 1) % 6;
if (angle == 0)
angle = 6;
}
}
}
if (r <= 100)
{
sub125::solve();
return 0;
}
sub6::solve();
// printclock;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNwcmFnbWEgR0NDIG9wdGltaXplKCJPMyx1bnJvbGwtbG9vcHMiKQojcHJhZ21hIEdDQyB0YXJnZXQoImF2eDIsYm1pLGJtaTIsbHpjbnQscG9wY250IikKI2RlZmluZSBGT1IoaSwgYSwgYikgZm9yIChpbnQgaSA9IChhKTsgaSA8PSAoYik7IGkrKykKI2RlZmluZSBGT0QoaSwgYSwgYikgZm9yIChpbnQgaSA9IChhKTsgaSA+PSAoYik7IGktLSkKI2RlZmluZSBSRVAoaSwgbikgZm9yIChpbnQgaSA9IDE7IGkgPD0gKG4pOyBpKyspCiNkZWZpbmUgUkVQMChpLCBuKSBmb3IgKGludCBpID0gMDsgaSA8IChuKTsgaSsrKQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIHByaW50Y2xvY2sgY2VyciA8PCAiXG5UaW1lIDogIiA8PCAxMDAwICogKGRvdWJsZSljbG9jaygpIC8gKGRvdWJsZSlDTE9DS1NfUEVSX1NFQyA8PCAibXNcbiI7CmNvbnN0IGludCBtb2QgPSAxZTkgKyA3LCBpbmYgPSAxMDAwMTExMDAwLCBteHIgPSAzMDUsIG14biA9IDJlNiArIDU7CmludCBhZGpbKGludCkzZTUgKyA1XVs3XTsKaW50IG9wcFs3XSwgbiwgcjsKYm9vbCBkZFtteG5dOwpsb25nIGxvbmcgYW5zID0gMDsKdmVjdG9yPGludD4gYm87Cm5hbWVzcGFjZSBzdWIxMjUKewogICAgdm9pZCBzb2x2ZSgpCiAgICB7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gMyAqIHIgKiAociArIDEpOyBpKyspCiAgICAgICAgewogICAgICAgICAgICB2ZWN0b3I8aW50PiB2dCg3LCBpKTsKICAgICAgICAgICAgd2hpbGUgKDEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludCBlID0gMTsKICAgICAgICAgICAgICAgIFJFUChqLCA2KQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gYWRqW3Z0W2pdXVtqXTsKICAgICAgICAgICAgICAgICAgICBpZiAodnRbal0gPT0gLTEgfHwgdnRbal0gPiAzICogKHIpICogKHIgKyAxKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGUgPSAtMTsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChkZFt2dFtqXV0pCiAgICAgICAgICAgICAgICAgICAgICAgIGUgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKGUgPT0gLTEpCiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGUgPT0gMSkKICAgICAgICAgICAgICAgICAgICAgICAgYW5zKys7CiAgICAgICAgICAgICAgICAgICAgaW50IHNlID0gdnRbMl07CiAgICAgICAgICAgICAgICAgICAgdmVjdG9yPGludD4gdnQyID0gdnQ7CiAgICAgICAgICAgICAgICAgICAgd2hpbGUgKDEpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpbnQgZSA9IDE7CiAgICAgICAgICAgICAgICAgICAgICAgIFJFUChqLCA2KQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnQgYW5nbGUgPSAoaiArIDIpICUgNjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChhbmdsZSA9PSAwKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFuZ2xlID0gNjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZ0MltqXSA9IGFkalt2dDJbal1dW2FuZ2xlXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChqID09IDEgJiYgdnQyW2pdID09IHNlKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGUgPSAtMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh2dDJbal0gPT0gLTEgfHwgdnQyW2pdID4gMyAqIChyKSAqIChyICsgMSkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZSA9IC0xOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGRkW3Z0MltqXV0pCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGUgPT0gLTEpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAoZSA9PSAxKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgYW5zKys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgYW5zIDw8ICdcbic7CiAgICB9Cn0KbmFtZXNwYWNlIHN1YjYKewogICAgaW50IGFkajJbKGludCkzZTUgKyA1XVsxMl1bN10sIGh1b25nWzMwNV1bN107CiAgICB2ZWN0b3I8aW50PiBwb3BiaXRbMTAwNV07CiAgICBpbmxpbmUgaW50IGdldChpbnQgeCwgaW50IGssIGludCBkKQogICAgewogICAgICAgIC8vIGNlcnIgPDwgeCA8PCAnICcgPDwgayA8PCAnICcgPDwgZCA8PCAnXG4nOwogICAgICAgIGlmICh4ID09IC0xKQogICAgICAgICAgICByZXR1cm4geDsKICAgICAgICBmb3IgKGF1dG8gaSA6IHBvcGJpdFtrXSkKICAgICAgICB7CiAgICAgICAgICAgIGlmICh4ID09IC0xKQogICAgICAgICAgICAgICAgcmV0dXJuIHg7CiAgICAgICAgICAgIHggPSBhZGoyW3hdW2ldW2RdOwogICAgICAgIH0KICAgICAgICByZXR1cm4geDsKICAgIH0KICAgIHZvaWQgc29sdmUoKQogICAgewogICAgICAgIFJFUChpLCByKQogICAgICAgIHsKICAgICAgICAgICAgYW5zICs9IGk7CiAgICAgICAgfQogICAgICAgIGFucyAqPSBhbnM7CiAgICAgICAgLy8gY2JpIHRydW9jIG1hbmcgYWRqMgogICAgICAgIG1lbXNldChhZGoyLCAtMSwgc2l6ZW9mIGFkajIpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IDMgKiByICogKHIgKyAxKTsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgUkVQKGosIDYpCiAgICAgICAgICAgIGFkajJbaV1bMF1bal0gPSBhZGpbaV1bal07CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGsgPSAxOyAoMSA8PCBrKSA8PSAxMDAwOyBrKyspCiAgICAgICAgewogICAgICAgICAgICBSRVAoaiwgNikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gMyAqIHIgKiAociArIDEpOyBpKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGFkajJbaV1bayAtIDFdW2pdID09IC0xKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgYWRqMltpXVtrXVtqXSA9IC0xOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgYWRqMltpXVtrXVtqXSA9IGFkajJbYWRqMltpXVtrIC0gMV1bal1dW2sgLSAxXVtqXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBSRVAoaSwgMTAwMCkKICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyAoMSA8PCBqKSA8PSBpOyBqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChpID4+IGogJiAxKQogICAgICAgICAgICAgICAgICAgIHBvcGJpdFtpXS5wYihqKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAvLyBzb2x2ZQogICAgICAgIHZlY3RvcjxpbnQ+IHZ0KDcpOwogICAgICAgIGZvciAoaW50IGkgOiBibykKICAgICAgICB7CiAgICAgICAgICAgIEZPUihoLCAwLCByKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBSRVAoZCwgNikKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBodW9uZ1toXVtkXSA9IGdldChpLCBoLCBkKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBSRVAobSwgcikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgUkVQMChoLCBtKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIC8vIEEKICAgICAgICAgICAgICAgICAgICBpbnQgc3RhcnQgPSBodW9uZ1toXVs2XTsKICAgICAgICAgICAgICAgICAgICBpbnQgdGFtID0gZ2V0KHN0YXJ0LCBtLCA0KTsKICAgICAgICAgICAgICAgICAgICBpbnQgZSA9IDE7CiAgICAgICAgICAgICAgICAgICAgUkVQKGosIDYpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB2dFtqXSA9IGdldCh0YW0sIG0sIGopOwogICAgICAgICAgICAgICAgICAgICAgICBpbnQgYW5nbGUgPSAoaiArIDIpICUgNjsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGFuZ2xlID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBhbmdsZSA9IDY7CiAgICAgICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gZ2V0KHZ0W2pdLCBoLCBhbmdsZSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICh2dFtqXSA9PSAtMSB8fCB2dFtqXSA+IDMgKiAocikgKiAociArIDEpKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlID0gLTE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoZSA9PSAxICYmIHZ0WzFdID09IGkpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucy0tOwogICAgICAgICAgICAgICAgICAgIC8vIEIKICAgICAgICAgICAgICAgICAgICBzdGFydCA9IGh1b25nW2hdWzFdOwogICAgICAgICAgICAgICAgICAgIHRhbSA9IGdldChzdGFydCwgbSwgNSk7CiAgICAgICAgICAgICAgICAgICAgZSA9IDE7CiAgICAgICAgICAgICAgICAgICAgUkVQKGosIDYpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB2dFtqXSA9IGdldCh0YW0sIG0sIGopOwogICAgICAgICAgICAgICAgICAgICAgICBpbnQgYW5nbGUgPSAoaiArIDIpICUgNjsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGFuZ2xlID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBhbmdsZSA9IDY7CiAgICAgICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gZ2V0KHZ0W2pdLCBoLCBhbmdsZSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICh2dFtqXSA9PSAtMSB8fCB2dFtqXSA+IDMgKiAocikgKiAociArIDEpKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlID0gLTE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoZSA9PSAxICYmICFkZFt2dFsxXV0gJiYgdnRbMl0gPT0gaSkKICAgICAgICAgICAgICAgICAgICAgICAgYW5zLS07CiAgICAgICAgICAgICAgICAgICAgLy8gQwogICAgICAgICAgICAgICAgICAgIHN0YXJ0ID0gaHVvbmdbaF1bMl07CiAgICAgICAgICAgICAgICAgICAgdGFtID0gZ2V0KHN0YXJ0LCBtLCA2KTsKICAgICAgICAgICAgICAgICAgICBlID0gMTsKICAgICAgICAgICAgICAgICAgICBSRVAoaiwgNikKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gZ2V0KHRhbSwgbSwgaik7CiAgICAgICAgICAgICAgICAgICAgICAgIGludCBhbmdsZSA9IChqICsgMikgJSA2OwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoYW5nbGUgPT0gMCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFuZ2xlID0gNjsKICAgICAgICAgICAgICAgICAgICAgICAgdnRbal0gPSBnZXQodnRbal0sIGgsIGFuZ2xlKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHZ0W2pdID09IC0xIHx8IHZ0W2pdID4gMyAqIChyKSAqIChyICsgMSkpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGUgPSAtMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChlID09IDEgJiYgIWRkW3Z0WzFdXSAmJiAhZGRbdnRbMl1dICYmIHZ0WzNdID09IGkpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucy0tOwogICAgICAgICAgICAgICAgICAgIC8vIEQKICAgICAgICAgICAgICAgICAgICBzdGFydCA9IGh1b25nW2hdWzNdOwogICAgICAgICAgICAgICAgICAgIHRhbSA9IGdldChzdGFydCwgbSwgMSk7CiAgICAgICAgICAgICAgICAgICAgZSA9IDE7CiAgICAgICAgICAgICAgICAgICAgUkVQKGosIDYpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB2dFtqXSA9IGdldCh0YW0sIG0sIGopOwogICAgICAgICAgICAgICAgICAgICAgICBpbnQgYW5nbGUgPSAoaiArIDIpICUgNjsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGFuZ2xlID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBhbmdsZSA9IDY7CiAgICAgICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gZ2V0KHZ0W2pdLCBoLCBhbmdsZSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICh2dFtqXSA9PSAtMSB8fCB2dFtqXSA+IDMgKiAocikgKiAociArIDEpKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlID0gLTE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoZSA9PSAxICYmICFkZFt2dFsxXV0gJiYgIWRkW3Z0WzJdXSAmJiAhZGRbdnRbM11dICYmIHZ0WzRdID09IGkpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucy0tOwogICAgICAgICAgICAgICAgICAgIC8vIEUKICAgICAgICAgICAgICAgICAgICBzdGFydCA9IGh1b25nW2hdWzRdOwogICAgICAgICAgICAgICAgICAgIHRhbSA9IGdldChzdGFydCwgbSwgMik7CiAgICAgICAgICAgICAgICAgICAgZSA9IDE7CiAgICAgICAgICAgICAgICAgICAgUkVQKGosIDYpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB2dFtqXSA9IGdldCh0YW0sIG0sIGopOwogICAgICAgICAgICAgICAgICAgICAgICBpbnQgYW5nbGUgPSAoaiArIDIpICUgNjsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGFuZ2xlID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBhbmdsZSA9IDY7CiAgICAgICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gZ2V0KHZ0W2pdLCBoLCBhbmdsZSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICh2dFtqXSA9PSAtMSB8fCB2dFtqXSA+IDMgKiAocikgKiAociArIDEpKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlID0gLTE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoZSA9PSAxICYmICFkZFt2dFsxXV0gJiYgIWRkW3Z0WzJdXSAmJiAhZGRbdnRbM11dICYmICFkZFt2dFs0XV0gJiYgdnRbNV0gPT0gaSkKICAgICAgICAgICAgICAgICAgICAgICAgYW5zLS07CiAgICAgICAgICAgICAgICAgICAgLy8gRgogICAgICAgICAgICAgICAgICAgIHN0YXJ0ID0gaHVvbmdbaF1bNV07CiAgICAgICAgICAgICAgICAgICAgdGFtID0gZ2V0KHN0YXJ0LCBtLCAzKTsKICAgICAgICAgICAgICAgICAgICBlID0gMTsKICAgICAgICAgICAgICAgICAgICBSRVAoaiwgNikKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHZ0W2pdID0gZ2V0KHRhbSwgbSwgaik7CiAgICAgICAgICAgICAgICAgICAgICAgIGludCBhbmdsZSA9IChqICsgMikgJSA2OwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoYW5nbGUgPT0gMCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFuZ2xlID0gNjsKICAgICAgICAgICAgICAgICAgICAgICAgdnRbal0gPSBnZXQodnRbal0sIGgsIGFuZ2xlKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHZ0W2pdID09IC0xIHx8IHZ0W2pdID4gMyAqIChyKSAqIChyICsgMSkpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGUgPSAtMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChlID09IDEgJiYgIWRkW3Z0WzFdXSAmJiAhZGRbdnRbMl1dICYmICFkZFt2dFszXV0gJiYgIWRkW3Z0WzRdXSAmJiAhZGRbdnRbNV1dICYmIHZ0WzZdID09IGkpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucy0tOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgYW5zIDw8ICdcbic7CiAgICB9Cn0KaW50MzJfdCBtYWluKCkKewojZGVmaW5lIHRhc2sgIiIKICAgIGlmIChmb3Blbih0YXNrICIuaW5wIiwgInIiKSkKICAgIHsKICAgICAgICBmcmVvcGVuKHRhc2sgIi5pbnAiLCAiciIsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKHRhc2sgIi5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICB9CiAgICBjaW4udGllKDApLT5zeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4gPj4gciA+PiBuOwogICAgaWYgKG4gPT0gMCkKICAgIHsKICAgICAgICBpbnQgYW5zID0gMDsKICAgICAgICBSRVAoaSwgcikKICAgICAgICB7CiAgICAgICAgICAgIGFucyArPSBpOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGFucyAqIGFucyA8PCAnXG4nOwogICAgICAgIHJldHVybiAwOwogICAgfQogICAgb3BwWzFdID0gNDsKICAgIG9wcFsyXSA9IDU7CiAgICBvcHBbM10gPSA2OwogICAgb3BwWzRdID0gMTsKICAgIG9wcFs1XSA9IDI7CiAgICBvcHBbNl0gPSAzOwogICAgbWVtc2V0KGFkaiwgLTEsIHNpemVvZiBhZGopOwogICAgUkVQKGksIG4pCiAgICB7CiAgICAgICAgaW50IHg7CiAgICAgICAgY2luID4+IHg7CiAgICAgICAgZGRbeF0gPSAxOwogICAgICAgIGJvLnBiKHgpOwogICAgfQogICAgUkVQKGksIDYpCiAgICB7CiAgICAgICAgYWRqWzBdW2ldID0gaTsKICAgICAgICBhZGpbaV1bb3BwW2ldXSA9IDA7CiAgICB9CiAgICBpbnQgcyA9IDI7CiAgICBSRVAoaSwgNikKICAgIHsKICAgICAgICBpbnQgbnVtaXRyID0gKGkgKyAxKSAlIDY7CiAgICAgICAgaWYgKG51bWl0ciA9PSAwKQogICAgICAgICAgICBudW1pdHIgPSA2OwogICAgICAgIHMgPSAocyArIDEpICUgNjsKICAgICAgICBpZiAocyA9PSAwKQogICAgICAgICAgICBzID0gNjsKICAgICAgICBhZGpbaV1bc10gPSBudW1pdHI7CiAgICAgICAgYWRqW251bWl0cl1bb3BwW3NdXSA9IGk7CiAgICB9CiAgICBSRVAoaSwgcikKICAgIHsKICAgICAgICBpbnQgeCA9IDMgKiAoaSArIDIpICogKGkgKyAxKSwgYW5nbGUgPSA2LCBzYW1lID0gMjsKICAgICAgICBmb3IgKGludCBpdHIgPSAzICogaSAqIChpIC0gMSkgKyAxLCBjbnQgPSAwOyBpdHIgPD0gMyAqIChpICsgMSkgKiBpOyBpdHIrKywgY250ID0gKGNudCArIDEpICUgaSkKICAgICAgICB7CiAgICAgICAgICAgIGludCBudW0gPSAoY250ID09IDApID8gMyA6IDI7CiAgICAgICAgICAgIGlmIChjbnQgPT0gMCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc2FtZSA9IChzYW1lICsgMSkgJSA2OwogICAgICAgICAgICAgICAgaWYgKHNhbWUgPT0gMCkKICAgICAgICAgICAgICAgICAgICBzYW1lID0gNjsKICAgICAgICAgICAgfQogICAgICAgICAgICBpbnQgbnVtYWRqID0gaXRyICsgMTsKICAgICAgICAgICAgaWYgKG51bWFkaiA9PSAzICogKGkgKyAxKSAqIGkgKyAxKQogICAgICAgICAgICAgICAgbnVtYWRqID0gMyAqIGkgKiAoaSAtIDEpICsgMTsKICAgICAgICAgICAgYWRqW2l0cl1bc2FtZV0gPSBudW1hZGo7CiAgICAgICAgICAgIGFkaltudW1hZGpdW29wcFtzYW1lXV0gPSBpdHI7CiAgICAgICAgICAgIGludCB4MiA9IHgsIGFuZ2xlMiA9IGFuZ2xlOwogICAgICAgICAgICBSRVAoaiwgbnVtKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBhZGpbaXRyXVthbmdsZTJdID0geDI7CiAgICAgICAgICAgICAgICBhZGpbeDJdW29wcFthbmdsZTJdXSA9IGl0cjsKICAgICAgICAgICAgICAgIGFuZ2xlMiA9IChhbmdsZTIgKyAxKSAlIDY7CiAgICAgICAgICAgICAgICBpZiAoYW5nbGUyID09IDApCiAgICAgICAgICAgICAgICAgICAgYW5nbGUyID0gNjsKICAgICAgICAgICAgICAgIHgyKys7CiAgICAgICAgICAgICAgICBpZiAoeDIgPT0gMyAqIChpICsgMikgKiAoaSArIDEpICsgMSkKICAgICAgICAgICAgICAgICAgICB4MiA9IDMgKiAoaSArIDEpICogaSArIDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgUkVQKGosIG51bSAtIDEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHgrKzsKICAgICAgICAgICAgICAgIGlmICh4ID09IDMgKiAoaSArIDIpICogKGkgKyAxKSArIDEpCiAgICAgICAgICAgICAgICAgICAgeCA9IDMgKiAoaSArIDEpICogaSArIDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgUkVQKGosIG51bSAtIDIpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGFuZ2xlID0gKGFuZ2xlICsgMSkgJSA2OwogICAgICAgICAgICAgICAgaWYgKGFuZ2xlID09IDApCiAgICAgICAgICAgICAgICAgICAgYW5nbGUgPSA2OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaWYgKHIgPD0gMTAwKQogICAgewogICAgICAgIHN1YjEyNTo6c29sdmUoKTsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIHN1YjY6OnNvbHZlKCk7CiAgICAvLyBwcmludGNsb2NrOwogICAgcmV0dXJuIDA7Cn0=