#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int>PII;
int ne[1000001], e[1000001], to[1000001];
int idx, h[1000001];
void add(int form, int th, int w) {
ne[++idx] = h[form], to[idx] = th, e[idx] = w;
h[form] = idx;
}
bool st[1000001];
char s[1000001], k;
int n, t = 0, d[10000001], minn = 120012012;
priority_queue<PII, vector<PII>, greater<PII>>q;
void dist(int x) {
memset(d, 0x3f3f, sizeof(d));
memset(st, 0, sizeof(st));
d[x] = 0;
q.push({x, 0});
while (!q.empty()) {
PII y = q.top();
q.pop();
int a = y.first;
if (st[a])
continue;
else
st[a] = 1;
for (int i = h[a]; i; i = ne[i]) {
int j = to[i];
if (d[j] > d[a] + e[i]) {
d[j] = d[a] + e[i];
if (!st[j]) {
q.push({j, d[j]});
}
}
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
char a, b;
int c;
cin >> a >> b >> c;
add(a - '0', b - '0', c);
add(b - '0', a - '0', c);
}
dist('Z' - '0');
for (int i = 'A'; i < 'Z'; i++) {
if (minn > d[i - '0']) {
minn = d[i - '0'];
k = i;
}
}
cout << k << " " << minn;
return 0;
}