#include "Anthony.h"
#include<bits/stdc++.h>
#define f0r(i,n) for(int i = 0; i < n; i++)
#define FOR(i,k,n) for(int i = k; i < n; i++)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define F first
#define S second
#define vi vector<int>
#define pii pair<int,int>
#define ist(x,y) ((x >> y) & 1)
#define dout(x) cout<<x<<' '<<#x<<endl
#define dout2(x,y) cout<<x<<' '<<#x<<' '<<y<<' '<<#y<<endl
#define vout(v) cout<<#v<<": "; for(auto u : v)cout<<u<<' '; cout<<endl
namespace {
int FunctionExample(int i, int A) {
return i % A;
}
} // namespace
using namespace std;
std::vector<int> Mark(int N, int M, int A, int B,
std::vector<int> U, std::vector<int> V) {
if(B==0){
std::vector<int> ret(M, -1);
vector<pair<int,int>>edges;
vector<int>adj[N];
f0r(i, M){
edges.pb({min(U[i], V[i]), max(U[i], V[i])});
adj[U[i]].pb(V[i]);
adj[V[i]].pb(U[i]);
}
map<pair<int,int>,int>m;
f0r(i, M){
m[edges[i]] = i;
}
vi dist(N, 1e9);
dist[0] = 0;
queue<int>q;
q.push(0);
while(!q.empty()){
int node = q.front();
q.pop();
for(auto u : adj[node]){
if(dist[u] == 1e9){
ret[m[{min(u, node), max(u, node)}]] = dist[node] % 3;
dist[u] = dist[node] + 1;
q.push(u);
}
}
}
f0r(i, M){
if(ret[i] == -1){
ret[i] = min(dist[edges[i].first], dist[edges[i].second]) % 3;
}
}
return ret;
}
else{
vector<pii>adj[N]; vi deg(N); f0r(i,M)adj[U[i]].eb(V[i],i),adj[V[i]].eb(U[i],i),deg[U[i]]++,deg[V[i]]++;
vi magic = {0, 1, 1, 0, 0, 1};
vi col(N); queue<int>q; q.push(0); vi vis(N), md(N); vis[0]=1; vi ans(M); while(!q.empty()){
int node = q.front(); q.pop(); for(auto [u,id] : adj[node])if(!vis[u]){
if(deg[node] != 2 || node == 0){
col[u] = col[node] ^ 1, ans[id] = col[u], q.push(u); vis[u] = 1; if(deg[u] == 2)md[u] = col[u];
}
else{
md[u] = (md[node] + 1) % 6; col[u] = magic[md[u]]; ans[id] = col[u]; q.push(u); vis[u] = 1;
}
}
}
return ans;
}
}
#include "Catherine.h"
#include<bits/stdc++.h>
#define f0r(i,n) for(int i = 0; i < n; i++)
#define FOR(i,k,n) for(int i = k; i < n; i++)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define F first
#define S second
#define vi vector<int>
#define pii pair<int,int>
#define ist(x,y) ((x >> y) & 1)
#define dout(x) cout<<x<<' '<<#x<<endl
#define dout2(x,y) cout<<x<<' '<<#x<<' '<<y<<' '<<#y<<endl
#define vout(v) cout<<#v<<": "; for(auto u : v)cout<<u<<' '; cout<<endl
// namespace {
//
// int A, B;
// int variable_example = 0;
//
// } // namespace
using namespace std;
int A, B, moves; bool mode; vi path;
vi magic = {0,1,1,0,0,1,0,1,1,0,0,1};
void Init(int A, int B) {
::A = A;
::B = B;
moves = 0;
}
int Move(std::vector<int> y) { //vout(y);
if(B == 0){
vector<int>has;
f0r(i, A){
if(y[i] != 0){
has.pb(i);
}
}
sort(has.begin(), has.end());
if(has.size() == 1){
return has[0];
}
else{
if(has[0] == 0 && has[1] == 1)return 0;
if(has[0] == 1 && has[1] == 2)return 1;
return 2;
}
}
else{
moves++;
if(moves == 1){
if(y[0] + y[1] == 2){
mode = 1; f0r(i,y[1])path.pb(1); f0r(i,y[0])path.pb(0); if(y[0] >= 1)return 0; return 1;
}
else{
mode = 0; if(y[0] == 1)return 0; return 1;
}
}
else{ //dout(mode);
if(mode){
if(y[0] + y[1] != 1){
mode = 0; return -1;
}
else{
f0r(i,y[1])path.pb(1); f0r(i,y[0])path.pb(0); if(path.size() == 5){ //vout(path);
f0r(i,8){
vi w; FOR(j,i,i+5)w.pb(magic[j]); if(path == w){
mode = 0; return -1;
}
}
mode = 0; if(y[0] == 1)return 0; return 1;
}
if(y[0] == 1)return 0; return 1;
}
}
else{
if(y[0] == 1)return 0; return 1;
}
}
}
}