#include <bits/stdc++.h>
using namespace std;
vector<pair<int, string> > Ar[1007];
bool tk[5][5];
int solve() {
  int cost = 1e9;
  if (Ar[3].size()) cost = Ar[3][0].first;
  bool a, b, c;
  a = b = c = false;
  int c1 = 0;
  int minA = 1e9, minB = 1e9, minC = 1e9;
  for (int i = 0; i < Ar[1].size(); i++) {
    if (Ar[1][i].second == "A" && !a)
      c1 += Ar[1][i].first, a = 1, minA = min(minA, Ar[1][i].first);
    if (Ar[1][i].second == "B" && !b)
      c1 += Ar[1][i].first, b = 1, minB = min(minB, Ar[1][i].first);
    if (Ar[1][i].second == "C" && !c)
      c1 += Ar[1][i].first, c = 1, minC = min(minC, Ar[1][i].first);
  }
  if (a && b && c) cost = min(cost, c1);
  int c2 = 0;
  a = b = c = false;
  for (int i = 0; i < Ar[2].size(); i++) {
    if (a && b && c) {
      cost = min(cost, c2);
      c2 = 0;
      a = 0, b = 0, c = 0;
    }
    if (Ar[2][i].second == "AB" && (!a || !b)) {
      a = 1;
      b = 1;
      c2 += Ar[2][i].first;
      if (minC != 1e9) c2 += minC, c = 1;
    }
    if (Ar[2][i].second == "AC" && (!a || !c)) {
      a = 1;
      c = 1;
      c2 += Ar[2][i].first;
      if (minB != 1e9) c2 += minB, b = 1;
    }
    if (Ar[2][i].second == "BC" && (!c || !b)) {
      c = 1;
      b = 1;
      c2 += Ar[2][i].first;
      if (minA != 1e9) c2 += minA, a = 1;
    }
  }
  if (a && b && c) cost = min(cost, c2);
  a = b = c = false;
  if (cost == 1e9) {
    cout << -1;
    exit(0);
  }
  return cost;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string SS;
    int val;
    cin >> val >> SS;
    sort(SS.begin(), SS.end());
    Ar[SS.size()].push_back({val, SS});
  }
  for (int i = 1; i <= 3; i++) sort(Ar[i].begin(), Ar[i].end());
  cout << solve();
  return 0;
}
