#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  long long i, j;
  string s;
  long long x, a1 = 1000000000, a2 = 1000000000, a3 = 1000000000, c[10001];
  long long n1[1001] = {0}, n2[1001] = {0}, n3[1001] = {0};
  for (i = 0; i < n; i++) {
    cin >> c[i] >> s;
    for (j = 0; j < s.size(); j++) {
      if (s[j] == 'A')
        n1[i]++;
      else if (s[j] == 'B')
        n2[i]++;
      else if (s[j] == 'C')
        n3[i]++;
    }
    if (n2[i] == 0 && n3[i] == 0 && n1[i] == 1) {
      a1 = min(a1, c[i]);
    }
    if (n2[i] == 1 && n3[i] == 0 && n1[i] == 0) {
      a2 = min(a2, c[i]);
    }
    if (n2[i] == 0 && n3[i] == 1 && n1[i] == 0) {
      a3 = min(a3, c[i]);
    }
  }
  long long ans = 1000000000;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (n1[i] + n1[j] >= 1 && n2[i] + n2[j] >= 1 && n3[i] + n3[j] >= 1) {
        ans = min(ans, c[i] + c[j]);
      }
    }
  }
  ans = min(ans, a1 + a2 + a3);
  for (i = 0; i < n; i++) {
    if (n3[i]) {
      ans = min(ans, a1 + a2 + c[i]);
    }
    if (n2[i]) {
      ans = min(ans, a1 + a3 + c[i]);
    }
    if (n1[i]) {
      ans = min(ans, a2 + a3 + c[i]);
    }
    if (n1[i] && n2[i] && n3[i]) {
      ans = min(ans, c[i]);
    }
  }
  if (ans > 300000) ans = -1;
  cout << ans << '\n';
}
