#include <bits/stdc++.h>
using namespace std;
bool vet[3];
int main() {
  long long n, a = 2e9, b = 2e9, c = 2e9, ab = 2e9, bc = 2e9, ac = 2e9,
               abc = 2e9;
  char fw;
  scanf("%lld", &n);
  for (long long i = 0, pr; i < n; i++) {
    scanf("%lld", &pr);
    scanf(" %c", &fw);
    vet[fw - 'A'] = true;
    while (1) {
      fw = getchar();
      if (fw < 'A' || fw > 'C') break;
      vet[fw - 'A'] = true;
    }
    if (vet[0] == true) {
      a = min(a, pr);
      if (vet[1] == true) {
        ab = min(ab, pr);
        if (vet[2] == true) {
          abc = min(abc, pr);
        }
      }
      if (vet[2] == true) {
        ac = min(ac, pr);
      }
    }
    if (vet[1] == true) {
      b = min(b, pr);
      if (vet[2] == true) {
        bc = min(bc, pr);
      }
    }
    if (vet[2] == true) {
      c = min(c, pr);
    }
    vet[0] = vet[1] = vet[2] = false;
    ;
  }
  abc = min(abc, ab + c);
  abc = min(abc, ac + b);
  abc = min(abc, bc + a);
  abc = min(abc, a + b + c);
  if (abc == 2e9)
    printf("-1");
  else
    printf("%lld", abc);
  return 0;
}
