#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, n;
  string str;
  cin >> t;
  long long abc = INT_MAX;
  long long ab = INT_MAX;
  long long bc = INT_MAX;
  long long ac = INT_MAX;
  long long a = INT_MAX;
  long long b = INT_MAX;
  long long c = INT_MAX;
  while (t--) {
    cin >> n >> str;
    sort(str.begin(), str.end());
    if (str == "ABC") {
      abc = min(abc, n);
    }
    if (str == "AB") {
      ab = min(ab, n);
    }
    if (str == "BC") {
      bc = min(bc, n);
    }
    if (str == "AC") {
      ac = min(ac, n);
    }
    if (str == "A") {
      a = min(a, n);
    }
    if (str == "B") {
      b = min(b, n);
    }
    if (str == "C") {
      c = min(c, n);
    }
  }
  if (min(abc,
          min(a + bc,
              min(b + ac,
                  min(c + ab,
                      min(a + b + c, min(ab + bc, min(ac + ab, bc + ac))))))) ==
      INT_MAX)
    cout << -1;
  else
    cout << min(
        abc, min(a + bc,
                 min(b + ac,
                     min(c + ab, min(a + b + c,
                                     min(ab + bc, min(ac + ab, bc + ac)))))));
}
