n_drop, cloth = int(input()), [[int(i) for i in input().split()] for _ in range(10)]
n_stain = sum(sum(i) for i in cloth)
n = (n_stain - 5 * n_drop) // 4

drops_candidate = []
dl = 0
while 2 * dl <= n:
    dm = n - 2 * dl
    ds = n_drop - dm - dl
    if ds >= 0:
        drops_candidate.append([ds, dm, dl])
    dl += 1

sdxy = ((0, 0), (-1, 0), (1, 0), (0, -1), (0, 1))
mdxy = sdxy + ((-1, -1), (-1, 1), (1, -1), (1, 1))
ldxy = mdxy + ((-2, 0), (2, 0), (0, -2), (0, 2))
dxys = (sdxy, mdxy, ldxy)


def find(cloth, drops, history):
    for d in range(2, -1, -1):
        if not drops[d]:
            continue
        (start, stop) = (1, 9) if d < 2 else (2, 8)
        for y in range(start, stop):
            for x in range(start, stop):
                for dxy in dxys[d]:
                    (dx, dy) = dxy
                    if not cloth[y + dy][x + dx]:
                        break
                else:
                    new_cloth, new_drops, new_history = [r[:] for r in cloth], drops[:], history[:]
                    for dxy in dxys[d]:
                        (dx, dy) = dxy
                        new_cloth[y + dy][x + dx] -= 1

                    new_drops[d] -= 1
                    new_history.append((x, y, d + 1))
                    if sum(new_drops):
                        result = find(new_cloth, new_drops, new_history)
                        if result:
                            return result
                    else:
                        return new_history
    return False


for drops in drops_candidate:
    history = find(cloth, drops, [])
    if history != False:
        for h in history:
            print(*h)
        break

### FIXED CODE ###

n_drop, cloth = int(input()), [[int(i) for i in input().split()] for _ in range(10)]
n_stain = sum(sum(i) for i in cloth)
n = (n_stain - 5 * n_drop) // 4

drops_candidate = []
dl = 0
while 2 * dl <= n:
    dm = n - 2 * dl
    ds = n_drop - dm - dl
    if ds >= 0:
        drops_candidate.append([ds, dm, dl])
    dl += 1

dx = (0, -1, 1, 0, 0, -1, -1, 1, 1, -2, 2, 0, 0)
dy = (0, 0, 0, -1, 1, -1, 1, -1, 1, 0, 0, -2, 2)
dd = (5, 9, 13)


def find(cloth, drops, pointer):
    global dy, dx, dd
    for p in range(pointer, 100):
        if cloth[p // 10][p % 10]:
            break
        pointer += 1
    for d in range(3):
        if not drops[d]:
            continue
        dn = dd[d]
        mir = int(d == 2)
        mar = 9 - mir
        for i in range(dn):
            x, y = pointer % 10 + dx[i], pointer // 10 + dy[i]
            if not mir < x < mar or not mir < y < mar:
                continue
            for j in range(dn):
                if not cloth[y + dy[j]][x + dx[j]]:
                    break
            else:
                for j in range(dn):
                    cloth[y + dy[j]][x + dx[j]] -= 1
                drops[d] -= 1
                if sum(drops):
                    result = find(cloth, drops, pointer)
                    if result:
                        result.append((x, y, d + 1))
                        return result
                    for i in range(dn):
                        cloth[y + dy[i]][x + dx[i]] += 1
                    drops[d] += 1
                else:
                    return [(x, y, d + 1)]
    return False


for drops in drops_candidate:
    history = find(cloth, drops, 0)
    if history != False:
        for h in history:
            print(*h)
        break