#include <bits/stdc++.h>
using namespace std;
long long i, j, k;
bool comp(pair<long long, string> a, pair<long long, string> b) {
  return a.first < b.first;
}
int main() {
  long long n;
  cin >> n;
  vector<pair<long long, string> > v(n);
  for (i = (0); i < (n); i++) {
    string p;
    cin >> v[i].first >> p;
    string pp = "000";
    for (j = (0); j < ((int)((p).size())); j++) pp[p[j] - 'A'] = '1';
    v[i].second = pp;
  }
  long long ans = 1000000000000000011LL;
  for (i = (0); i < (n); i++) {
    string pp = v[i].second;
    if (pp[0] == '1' && pp[1] == '1' && pp[2] == '1')
      ans = min(ans, v[i].first);
  }
  long long totCost = 0;
  for (j = (0); j < (3); j++) {
    long long currCost = 1000000000000000011LL;
    for (i = (0); i < (n); i++) {
      string pp = v[i].second;
      if (pp[j] == '1' && pp[(j + 1) % 3] == '0' && pp[(j + 1) % 3] == '0')
        currCost = min(currCost, v[i].first);
    }
    totCost += currCost;
  }
  ans = min(ans, totCost);
  for (i = (0); i < (n); i++) {
    string pp1 = v[i].second;
    for (j = (0); j < (n); j++) {
      string pp2 = v[j].second;
      string ppp = "000";
      for (k = (0); k < (3); k++)
        if (pp1[k] == '1' || pp2[k] == '1') ppp[k] = '1';
      bool flag = true;
      for (k = (0); k < (3); k++)
        if (ppp[k] == '0') flag = false;
      if (flag) ans = min(ans, v[i].first + v[j].first);
    }
  }
  if (ans == 1000000000000000011LL)
    cout << "-1" << '\n';
  else
    cout << ans << '\n';
  return 0;
}
