In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions:  overall, there must be m dances; exactly three people must take part in each dance; each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland). 

The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance. 

You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.


-----Input-----

The first line contains two space-separated integers n (3 ≤ n ≤ 10^5) and m (1 ≤ m ≤ 10^5) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.


-----Output-----

Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.


-----Examples-----
Input
7 3
1 2 3
1 4 5
4 6 7

Output
1 2 3 3 2 2 1 

Input
9 3
3 6 9
2 5 8
1 4 7

Output
1 1 1 2 2 2 3 3 3 

Input
5 2
4 1 5
3 1 2

Output
2 3 1 1 3

Difficulty: interview

Starter code:

Solutions:
import sys;

nbDancers, nbDances = list(map(int, sys.stdin.readline().split(' ')));
dancers = [0]*(nbDancers+1);

for line in sys.stdin:
	taken = [False, False, False, False];

	ds = list(map(int, line.split(' ')));

	for d in ds:
		taken[dancers[d]] = True;

	for d in ds:
		if dancers[d] == 0:
			if not taken[1]:
				dancers[d] = 1;
				taken[1] = True;
			elif not taken[2]:
				dancers[d] = 2;
				taken[2] = True;
			elif not taken[3]:
				dancers[d] = 3;
				taken[3] = True;

print((' '.join(map(str, dancers[1:]))));



n, m = map(int, input().split(" "))

color = n*[0]

for i in range(0, m) :
	a, b, c = map(lambda a: int(a)-1, input().split(" "))
	if color[a] != 0 :
		color[b] = color[a]%3 + 1
		color[c] = (color[a]+1)%3 + 1
	elif color[b] != 0 :
		color[a] = color[b]%3 + 1
		color[c] = (color[b]+1)%3 + 1
	elif color[c] != 0 :
		color[b] = color[c]%3 + 1
		color[a] = (color[c]+1)%3 + 1
	else :
		color[a] = 1
		color[b] = 2
		color[c] = 3

print(' '.join(map(str, color)))

import sys
n,m=list(map(int,sys.stdin.readline().split()))

Colors=[0]*(n+1)

for i in range(m):
    a,b,c=list(map(int,sys.stdin.readline().split()))
    Z=[a,b,c]
    Q=[1,2,3]
    if(Colors[a]!=0):
        Z.remove(a)
        Q.remove(Colors[a])
    if(Colors[b]!=0):
        Z.remove(b)
        Q.remove(Colors[b])
    if(Colors[c]!=0):
        Z.remove(c)
        Q.remove(Colors[c])
    for i in range(len(Z)):
        Colors[Z[i]]=Q[i]


Ans=""
Ans+=str(Colors[1])
for i in range(2,n+1):
    Ans+=" "+str(Colors[i])
sys.stdout.write(Ans)
    


#FlagDay

data = input().split(" ")
dances = int(data[1])
totalDancers = int(data[0])

dict = {}
for d in range(dances):
	dancers = input().split(" ")
	dancers = [int(x) for x in dancers]
	i = 0
	while i<3:
		dict.setdefault(dancers[i], i)
		if dancers[i] in dict:
			if dict[dancers[i]] != i:
				dancers[dict[dancers[i]]], dancers[i] = dancers[i], dancers[dict[dancers[i]]]
				dict[dancers[i]] = i
		else:
			dict.setdefault(dancers[i], i)
		i += 1
ans = ""
for i in range(1, totalDancers):
	ans = ans + str(dict[i]+1) + " "
ans = ans + str(dict[totalDancers]+1)
print(ans)

import sys;

nbDancers, nbDances = list(map(int, sys.stdin.readline().split(' ')));
dancers = [0]*(nbDancers+1);

for line in sys.stdin:
	taken = [False, False, False, False];

	ds = list(map(int, line.split(' ')));

	for d in ds:
		taken[dancers[d]] = True;

	for d in ds:
		if dancers[d] == 0:
			iCurr = 1;
			while iCurr <= 3 and taken[iCurr]:
				iCurr += 1;

			dancers[d] = iCurr;
			taken[iCurr] = True;

print((' '.join(map(str, dancers[1:]))));


n,m=list(map(int,input().split()))
a=[0]*n
for i in range(m):
    s=list(map(int,input().split()))
    for j in range(1,4):
        if(a[s[j-1]-1]!=0):
            a[s[a[s[j-1]-1]-1]-1]=j
        else:
            a[s[j-1]-1]=j  
print(' '.join(map(str,a)))


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 16 20:35:46 2013

@author: Praveen Kumar
"""
import sys

x,n = list(map(int, input().split()))
d = {}
for i in range(n):
    a = list(map(int, input().split()))
    b = [1,2,3]
    rf = bf = wf = 0
    for j in a:
        if j in d:
            b.remove(d[j])
            a.remove(j)
    if len(b) != 0:
        for j in range(len(a)):
            d[a[j]] = b[j]

for i in range(1,len(d)+1):
    sys.stdout.write(str(d[i])+" ")


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 16 20:35:46 2013

@author: Praveen Kumar
"""
import sys

m,n = map(int, input().split())
d = [0]*(m+1);
for i in range(n):
    a = list(map(int, input().split()))
    b = [1,2,3]
    for j in a:
        if d[j] != 0:
            b.remove(d[j])
            a.remove(j)
    if len(b) != 0:
        for j in range(len(a)):
            d[a[j]] = b[j]

#for i in range(1,len(d)):
#    sys.stdout.write(str(d[i])+" ")
print(' '.join(map(str, d[1:])));  

def readln():
    return tuple(map(int, input().split()))

n, m = readln()
ans = [0] * (n + 1)
for _ in range(m):
    a = readln()
    for i in range(3):
        if ans[a[i]] == 1:
            ans[a[(i + 1) % 3]] = 2
            ans[a[(i + 2) % 3]] = 3
            break
        elif ans[a[i]] == 2:
            ans[a[(i + 1) % 3]] = 1
            ans[a[(i + 2) % 3]] = 3
            break
        elif ans[a[i]] == 3:
            ans[a[(i + 1) % 3]] = 1
            ans[a[(i + 2) % 3]] = 2
            break
    if not ans[a[0]]:
        ans[a[0]] = 1
        ans[a[1]] = 2
        ans[a[2]] = 3
print(' '.join(map(str, ans[1:])))


n, m = map(int, input().split())

colors = {}

for i in range(m):
	a, b, c = map(int, input().split())
	if a not in colors and b not in colors and c not in colors:
		colors[a] = 1
		colors[b] = 2
		colors[c] = 3
	else:
		l = [1, 2, 3]
		if a in colors:
			l.remove(colors[a])
			colors[b], colors[c] = l[0], l[1]
		elif b in colors:
			l.remove(colors[b])
			colors[a], colors[c] = l[0], l[1]
		else:
			l.remove(colors[c])
			colors[a], colors[b] = l[0], l[1]

for i in colors:
	print(colors[i], end=' ')


n, m = map(int, input().split())
colors = [0] * n

def setColors(a):
  for i in a:
    if colors[i - 1] == 0:
      for c in range(1, 4):
        for j in a:
          if i != j and c == colors[j - 1]:
            break
        else:
          colors[i - 1] = c
          break

for i in range(m):
  setColors(list(map(int, input().split())))

print(' '.join(map(str, colors)))

n, m = map(int, input().split())
t = [0] * (n + 1)
for i in range(m):
    a, b, c = map(int, input().split())
    if t[a]:
        if t[a] == '1': t[b], t[c] = '2', '3'
        elif t[a] == '2': t[b], t[c] = '1', '3'
        else: t[b], t[c] = '1', '2'
    elif t[b]:
        if t[b] == '1': t[a], t[c] = '2', '3'
        elif t[b] == '2': t[a], t[c] = '1', '3'
        else: t[a], t[c] = '1', '2'
    elif t[c]:
        if t[c] == '1': t[a], t[b] = '2', '3'
        elif t[c] == '2': t[a], t[b] = '1', '3'
        else: t[a], t[b] = '1', '2'
    else: t[a], t[b], t[c] = '1', '2', '3'
print(' '.join(t[1:]))

n, m = map(int, input().split())
t = [0] * (n + 1)
for j in range(m):
    a, b, c = map(int, input().split())
    x = t[a] | t[b] | t[c]
    for i in (a, b, c):
        if not t[i]:
            if not x & 1: t[i] = 1; x += 1;
            elif not x & 2: t[i] = 2; x += 2;
            else: t[i] = 4
p = {0: '1 ', 1: '1 ', 2: '2 ', 4: '3 '}
print(''.join(p[i] for i in t[1:]))

n, m = map(int, input().split())

t = [0] * (n + 1)
p = [0] * (n + 1)
q = [[] for i in range(n + 1)] 

def f(x):
    for i in q[x]:
        u, v = i[0], i[1]
        if t[u] and t[v]: continue
        if t[u] and not t[v]:
            t[v] = 7 - t[x] - t[u]
            for j in q[v]:
                p[j[0]] |= t[v]
                p[j[1]] |= t[v]
            #f(u)
            f(v)
        elif not t[u] and t[v]:
            t[u] = 7 - t[x] - t[v]
            for j in q[u]:
                p[j[0]] |= t[u]
                p[j[1]] |= t[u]
            f(u)
            #f(v)
        else:
            for k in [1, 2, 4]:                
                t[u], t[v] = k, 7 - t[x] - k
                if not (p[u] & t[u] or p[v] & t[v]): break                    
            
            for j in q[v]:
                p[j[0]] |= t[v]
                p[j[1]] |= t[v]                
            for j in q[u]:
                p[j[0]] |= t[u]
                p[j[1]] |= t[u]
            f(u)
            f(v)

if m == 49999:
    for j in range(m):
        a, b, c = map(int, input().split())
        x = t[a] | t[b] | t[c]
        for i in (a, b, c):
            if not t[i]:
                if not x & 1: t[i] = 1; x += 1;
                elif not x & 2: t[i] = 2; x += 2;
                else: t[i] = 4
else:           
    for i in range(m):
        a, b, c = map(int, input().split())

        q[a].append((b, c))
        q[b].append((a, c))
        q[c].append((a, b))
        
    for x in range(1, n + 1):
        if not t[x]:
            t[x] = 1
            for j in q[x]:
                p[j[0]] |= 1
                p[j[1]] |= 1
            f(x)
        
p = {0: '1 ', 1: '1 ', 2: '2 ', 4: '3 '}
print(''.join(p[x] for x in t[1:]))

#! /usr/bin/env python

n, m = [int(x) for x in input().split()]
dancers = {}
for i in range(m):
    dance = [int (x) for x in input().split()]
    for j in range(3):
        if dance[j] not in dancers:
            dancers[dance[j]] = j
        else:
            dancers[dance[(j + 1) % 3]] = (dancers[dance[j]] + 1) % 3
            dancers[dance[(j + 2) % 3]] = (dancers[dance[j]] + 2) % 3
            break

print(*[x + 1 for x in list(dancers.values())])



def __starting_point():
    n, m = input().split()
    n, m = int(n), int(m) 
    d = [] 
    color = dict() 
    for i in range(m) :
        d.append(input().split()) 
    for i in range(len(d)) :
        for j in range(len(d[i])): 
            d[i][j] = int(d[i][j])

    for i in range(m):    
        if i == 0 :
            for j in range(3) : 
                color[d[i][j]] = j+1 
        else :
            temp =dict() 
            for j in range(3) :
                if d[i][j] in color: 
                    temp[color[d[i][j]]] =1 
            for j in range(3) : 
                if  d[i][j] not in color : 
                    for k in range(3):
                        if k+1 not in temp:
                            color[d[i][j]] = k+1 
                            temp[k+1] =1 
                            break 
    for i in range(n) :
        print(color[i+1],end=' ') 
    print() 

__starting_point()


def __starting_point():
    n, m = map(int, input().split() ) 
    color = dict() 
    for i in range(m):    
        d = list(map(int, input().split()))
        temp = dict() 
        for j in range(3) :
            if d[j] in color: 
                temp[color[d[j]]] =1 
        for j in range(3) : 
            if  d[j] not in color : 
                for k in range(3):
                    if k+1 not in temp:
                        color[d[j]] = k+1 
                        temp[k+1] =1 
                        break 
    for i in range(n) :
        print(color[i+1],end=' ') 
    print() 

__starting_point()

R = lambda : map(int, input().split() ) 
n, m = R() 
color = dict() 
for i in range(m):    
    d = list(R())
    temp =0 
    for j in  d :
        if j  in color: 
            temp  = temp | ( 1 << color[j] ) 
    for j in range(3) : 
        if  d[j] not in color : 
            for k in range(3):
                if not ( (1 << (k+1) ) &  temp ) :
                    color[d[j]] = k+1 
                    temp  = temp | ( 1 << k+1 ) 
                    break 
for i in range(n): 
    print(color[i+1],end = ' ') 
print() 


R = lambda : list(map(int, input().split() )) 
n, m = R() 
color = [0]*(n) 
for i in range(m):    
    d = list(R())
    temp =0 
    for j in  d :
        if color[j-1] != 0 :
            temp  = temp | ( 1 << color[j-1] ) 
    for j in d : 
        if  color[j-1] == 0 : 
            for k in range(3):
                if not ( (1 << (k+1) ) &  temp ) :
                    color[j-1] = k+1 
                    temp  = temp | ( 1 << k+1 ) 
                    break 
print(' '.join(map(str, color)) )


n,m=map(int,input().split())
dancers={'auther':'wanglizhi'}
for i in range(m):
    a,b,c=map(int,input().split())
    if(a in dancers):
       color=dancers[a]
       dancers[b]=(color+1)%3
       dancers[c]=(color+2)%3
    elif(b in dancers):
       color=dancers[b]
       dancers[a]=(color+1)%3
       dancers[c]=(color+2)%3
    elif(c in dancers):
       color=dancers[c]
       dancers[a]=(color+1)%3
       dancers[b]=(color+2)%3
    else:
        dancers[a]=0
        dancers[b]=1
        dancers[c]=2
for i in range(n-1):
    print((dancers[i+1]+1),end=' ')
print(dancers[n]+1)



n,m=map(int,input().split())
l=[0]*(n+1)
for i in range(m):
    a,b,c=map(int,input().split())
    if(l[a]!=0):
        if(l[a]==1):
            l[b]=2
            l[c]=3
        elif(l[a]==2):
            l[b]=3
            l[c]=1
        else:
            l[b]=1
            l[c]=2
    elif(l[b]!=0):
        if(l[b]==1):
            l[c]=2
            l[a]=3
        elif(l[b]==2):
            l[c]=3
            l[a]=1
        else:
            l[c]=1
            l[a]=2
    elif(l[c]!=0):
        if(l[c]==1):
            l[a]=2
            l[b]=3
        elif(l[c]==2):
            l[a]=3
            l[b]=1
        else:
            l[a]=1
            l[b]=2
    else:
        l[a]=1
        l[b]=2
        l[c]=3
for i in range(1,n+1):
    print(l[i],end=' ')


def main():
    n, m = [int(i) for i in input().split()]
    
    colors = [0 for i in range(n + 1)]
    for i in range(m):
        arr = [int(i) for i in input().split()]
        early = colors[arr[0]] or colors[arr[1]] or colors[arr[2]]
        l = [1, 2, 3]
        if early: l.remove(early)
        for i in arr:
            if colors[i] == 0:
                colors[i] = l.pop()
    
    print(' '.join(str(colors[i]) for i in range(1, n + 1)))
    
main()


def intinp():
    return list(map(int,input().split()))
n,m = intinp()
Color = [0 for i in range(n+1)]
for i in range(m):
    u=[0]
    u=u+intinp()
    if Color[u[1]]:
        u[Color[u[1]]],u[1]=u[1],u[Color[u[1]]]
    if Color[u[2]]:
        u[Color[u[2]]],u[2]=u[2],u[Color[u[2]]]
    if Color[u[3]]:
        u[Color[u[3]]],u[3]=u[3],u[Color[u[3]]]
    Color[u[1]]=1
    Color[u[2]]=2
    Color[u[3]]=3
print(" ".join(list(map(str,Color[1:]))))

def main():
    n, m = list(map(int, input().split()))
    r = [0] * (n + 1)
    for _ in range(m):
        a, b, c = list(map(int, input().split()))
        r[a], r[b], r[c] = {
            (0, 0, 0): (1, 2, 3), (0, 0, 1): (2, 3, 1), (0, 0, 2): (1, 3, 2), (0, 0, 3): (1, 2, 3),
            (0, 1, 0): (2, 1, 3), (0, 1, 2): (3, 1, 2), (0, 1, 3): (2, 1, 3), (0, 2, 0): (1, 2, 3),
            (0, 2, 1): (3, 2, 1), (0, 2, 3): (1, 2, 3), (0, 3, 0): (1, 3, 2), (0, 3, 1): (2, 3, 1),
            (0, 3, 2): (1, 3, 2), (1, 0, 0): (1, 2, 3), (1, 0, 2): (1, 3, 2), (1, 0, 3): (1, 2, 3),
            (1, 2, 0): (1, 2, 3), (1, 2, 3): (1, 2, 3), (1, 3, 0): (1, 3, 2), (1, 3, 2): (1, 3, 2),
            (2, 0, 0): (2, 1, 3), (2, 0, 1): (2, 3, 1), (2, 0, 3): (2, 1, 3), (2, 1, 0): (2, 1, 3),
            (2, 1, 3): (2, 1, 3), (2, 3, 0): (2, 3, 1), (2, 3, 1): (2, 3, 1), (3, 0, 0): (3, 1, 2),
            (3, 0, 1): (3, 2, 1), (3, 0, 2): (3, 1, 2), (3, 1, 0): (3, 1, 2), (3, 1, 2): (3, 1, 2),
            (3, 2, 0): (3, 2, 1), (3, 2, 1): (3, 2, 1)}[r[a], r[b], r[c]]
    print(' '.join((None, "1", "2", "3")[x] for x in r[1:]))


def __starting_point():
    main()

__starting_point()

n, m = list(map(int, input().split()))
d = [1] * n
s = set()
for i in range(m):
    a, b, c = list(map(int, input().split()))
    if a - 1in s:
        if d[a - 1] == 1:
            d[b - 1] = 2
            d[c - 1] = 3
        
        if d[a - 1] == 2:
            d[b - 1] = 1
            d[c - 1] = 3
        
        if d[a - 1] == 3:
            d[b - 1] = 1
            d[c - 1] = 2
    
    if b - 1 in s:
        if d[b - 1] == 1:
            d[a - 1] = 2
            d[c - 1] = 3
        
        if d[b - 1] == 2:
            d[a - 1] = 1
            d[c - 1] = 3
        
        if d[b - 1] == 3:
            d[a - 1] = 1
            d[c - 1] = 2
    
    if c - 1 in s:
        if d[c - 1] == 1:
            d[b - 1] = 2
            d[a - 1] = 3
        
        if d[c - 1] == 2:
            d[b - 1] = 1
            d[a - 1] = 3
        
        if d[c - 1] == 3:
            d[b - 1] = 1
            d[a - 1] = 2
    if a - 1 not in s and b - 1not in s and c - 1 not in s:
        d[a - 1] = 1
        d[b - 1] = 2
        d[c - 1] = 3
    s.add(a - 1)
    s.add(b - 1)
    s.add(c - 1)
        
print(*d)