#!/usr/bin/env python3
from collections import deque
import sys

sys.setrecursionlimit(10001)

n = int(input())
m = n
if m > n:
    print("impossible")
else:
    Adj = [[] for _ in range(n)]
    E = {}
    Edges = []
    for _ in range(m):
        u, v, h = [int(x) for x in input().split()]
        u -= 1
        v -= 1
        E[(u, v)] = h
        E[(v, u)] = h
        Edges.append((u, v))
        Adj[u].append(v)
        Adj[v].append(u)

    # find cycle
    NEW, MARKED, DONE = 0, 1, 2
    color = {x: NEW for x in range(n)}
    prev = [-1] * n
    st = [0]
    found_cycle = False
    cycle_start = -1
    while len(st) > 0:
        u = st[-1]
        if color[u] != MARKED:
            color[u] = MARKED
            for v in Adj[u]:
                if v == prev[u]:
                    continue
                if color[v] == NEW:
                    st.append(v)
                    prev[v] = u
                elif color[v] == MARKED:
                    # found a cycle
                    found_cycle = True
                    cycle_start = v
                    prev[v] = u
                    break
            if found_cycle:
                break
        else:
            st.pop()
            color[u] = DONE

    cycle = [cycle_start]
    while prev[cycle[-1]] != cycle[0]:
        cycle.append(prev[cycle[-1]])

    # orient edges
    visited = [False] * n
    q = deque()
    D = {}

    for i, u in enumerate(cycle):
        v = cycle[(i + 1) % len(cycle)]
        D[(u, v)] = (u, v)
        D[(v, u)] = (u, v)
        q.append(u)
        visited[u] = True

    while len(q) > 0:
        u = q.popleft()
        for v in Adj[u]:
            if not visited[v]:
                D[(u, v)] = (v, u)
                D[(v, u)] = (v, u)
                visited[v] = True
                q.append(v)

    def check(D):
        H = [set() for _ in range(n)]
        for u, v in D:
            if u < v:
                if D[(u, v)] == (u, v):
                    if 1 in H[u] or E[(u, v)] in H[v]:
                        return False
                    H[u].add(1)
                    H[v].add(E[(u, v)])
                else:
                    if 1 in H[v] or E[(u, v)] in H[u]:
                        return False
                    H[u].add(E[(u, v)])
                    H[v].add(1)
        return True

    if check(D):
        for u, v in Edges:
            if D[(u, v)] == (u, v):
                print(u + 1)
            else:
                print(v + 1)
    else:
        print("impossible")
