from collections import defaultdict

road = int(eval(input()))

while road > 0:

    connect = defaultdict(list)

    # weight = defaultdict(defaultdict(float("inf")))

    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]

    for _ in range(road):

        c1, c2, w = (int(n) for n in input().split(" "))

        connect[c1].append(c2)

        connect[c2].append(c1)

        answer[c1][c2] = w

        answer[c2][c1] = w

    city = max(connect.keys()) + 1

    for i in range(city):

        for j in range(city):

            for k in range(city):

                answer[j][k] = min(answer[j][k], answer[j][i] + answer[i][k])

    sum_ans = [-1 , float("inf")]

    for i in range(city):

        if sum(answer[i][:city]) < sum_ans[1]:

            sum_ans = [i, sum(answer[i][:city])]

    print((*sum_ans))

    road = int(eval(input()))
road = int(eval(input()))

while road > 0:

    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]

    city = 0

    for _ in range(road):

        c1, c2, w = (int(n) for n in input().split(" "))

        answer[c1][c2] = w

        answer[c2][c1] = w

        city = max(city, c1, c2)

    city += 1

    for i in range(city):

        for j in range(city):

            for k in range(city):

                answer[j][k] = min(answer[j][k], answer[j][i] + answer[i][k])

    sum_ans = [-1 , float("inf")]

    for i in range(city):

        if sum(answer[i][:city]) < sum_ans[1]:

            sum_ans = [i, sum(answer[i][:city])]

    print((*sum_ans))

    road = int(eval(input()))
from collections import defaultdict

road = int(eval(input()))
while road > 0:
    connect = defaultdict(list)
    # weight = defaultdict(defaultdict(float("inf")))
    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]
    for _ in range(road):
        c1, c2, w = (int(n) for n in input().split(" "))
        connect[c1].append(c2)
        connect[c2].append(c1)
        answer[c1][c2] = w
        answer[c2][c1] = w
    city = max(connect.keys()) + 1
    for i in range(city):
        for j in range(city):
            for k in range(city):
                answer[j][k] = min(answer[j][k], answer[j][i] + answer[i][k])
    sum_ans = [-1, float("inf")]
    for i in range(city):
        if sum(answer[i][:city]) < sum_ans[1]:
            sum_ans = [i, sum(answer[i][:city])]
    print((*sum_ans))
    road = int(eval(input()))

road = int(eval(input()))
while road > 0:
    answer = [[float("inf") if n != m else 0 for n in range(10)] for m in range(10)]
    city = 0
    for _ in range(road):
        c1, c2, w = (int(n) for n in input().split(" "))
        answer[c1][c2] = w
        answer[c2][c1] = w
        city = max(city, c1, c2)
    city += 1
    for i in range(city):
        for j in range(city):
            for k in range(city):
                answer[j][k] = min(answer[j][k], answer[j][i] + answer[i][k])
    sum_ans = [-1, float("inf")]
    for i in range(city):
        if sum(answer[i][:city]) < sum_ans[1]:
            sum_ans = [i, sum(answer[i][:city])]
    print((*sum_ans))
    road = int(eval(input()))

-from collections import defaultdict - -    connect = defaultdict(list) -    # weight = defaultdict(defaultdict(float("inf"))) +    city = 0 -        connect[c1].append(c2) -        connect[c2].append(c1) -    city = max(connect.keys()) + 1 +        city = max(city, c1, c2) +    city += 1