#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
int n, mx[3], c[2000], ans[3], used[3];
string s[2000];
int main() {
  cin >> n;
  for (int i = 0; i < 3; ++i) mx[i] = INF;
  for (int i = 0; i < n; ++i) {
    cin >> c[i] >> s[i];
    for (int j = 0; j < s[i].length(); ++j) {
      if (mx[s[i][j] - 'A'] > c[i]) {
        mx[s[i][j] - 'A'] = c[i];
        ans[s[i][j] - 'A'] = i;
      }
    }
  }
  int sum = INF;
  for (int i = 0; i < n; ++i) {
    if (s[i].length() == 3) {
      sum = min(sum, c[i]);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = 0; k < 3; ++k) used[k] = 0;
      for (int k = 0; k < s[i].length(); ++k) used[s[i][k] - 'A'] = true;
      for (int k = 0; k < s[j].length(); ++k) used[s[j][k] - 'A'] = true;
      vector<int> lf;
      for (int k = 0; k < 3; ++k) {
        if (!used[k]) lf.push_back(k);
      }
      if (lf.empty())
        sum = min(sum, c[i] + c[j]);
      else {
        int tmp = c[i] + c[j];
        for (int k = 0; k < lf.size(); ++k) tmp += mx[lf[k]];
        sum = min(sum, tmp);
      }
    }
  }
  if (sum == INF) sum = -1;
  cout << sum;
}
