#!/usr/bin/env python3
import sys
import subprocess
sys.setrecursionlimit(100000000)

name = sys.argv[1]

n = int(sys.stdin.readline())
if n > 100:
    sys.exit(0)

es = []
neigh = [[] for i in range(n)]
all_neigh = [[] for i in range(n)]
term = [None]*n
stack = []
for i in range(n):
    fire, hire = map(int, sys.stdin.readline().split())
    while fire > 0:
        other, other_hire = stack.pop()
        mn = min(fire, other_hire)
        assert mn > 0
        term[i] = other
        es.append((other, i))
        neigh[other].append(i)
        all_neigh[other].append(i)
        all_neigh[i].append(other)
        other_hire -= mn
        fire -= mn
        if other_hire > 0:
            stack.append((other, other_hire))
    if hire > 0:
        stack.append((i, hire))

def different(S):
    c = 1
    while c in S:
        c += 1
    return c

color = [None]*n

def bipartite(cur):
    for nxt in all_neigh[cur]:
        if color[nxt] is None:
            color[nxt] = different({color[cur]})
            bipartite(nxt)
        if color[cur] == color[nxt]:
            return False
    return True

bip = True
for i in range(n):
    if color[i] is None:
        color[i] = 1
        S = [i]
        while S and bip:
            cur = S.pop()
            for nxt in all_neigh[cur]:
                if color[nxt] is None:
                    color[nxt] = different({color[cur]})
                    S.append(nxt)
                if color[cur] == color[nxt]:
                    bip = False
                    break

if not bip:
    color = [None]*n
    color[0] = 1
    S = [(0,3)]

    while S:
        (l, above_color) = S.pop()

        if l == n:
            continue

        if not neigh[l]:
            if l + 1 < n and term[l+1] is None:
                color[l+1] = different({above_color})
                S.append((l+1, above_color))
            continue

        r = neigh[l].pop()
        if term[r] == l:
            color[r] = different({color[l], above_color})
        else:
            assert color[r] is not None
        if term[r] == l:
            S.append((r, above_color))
        S.append((l, color[r]))

with open('%s.tex' % name, 'w') as f:
    f.write(r'''
\documentclass[tikz]{standalone}

\usepackage[latin1]{inputenc}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}

''')

    color_names = ['red', 'yellow', 'green']
    for i in range(0,n):
        f.write('\\node[circle,fill=%s] (%d) at (%d,0) {%d};\n' % (color_names[color[i]-1], i+1,i+1,i+1))
    for (a,b) in es:
        f.write('\\path[bend left=50] (%d) edge (%d);\n' % (a+1,b+1))

    f.write('''
\end{tikzpicture}

\end{document}
''')

try:
    subprocess.check_call(['pdflatex', '%s.tex' % name])
    subprocess.check_call(['convert', '-density', '150', '%s.pdf' % name, '-quality', '90', '-background', 'white', '-flatten', '%s.png' % name])
except:
    pass

