#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

struct State {
    State(int i = 0, int l = 0, int d = 0) : idx(i), len(l), dir(d) {

    } 
    int idx;
    int len;
    int dir;
};


int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(12);


    int n;
    cin >> n;
    vector<string> S(n);
    vector<long long> C(n);
    for (int i = 0; i < n; ++i) {
        cin >> S[i] >> C[i];
    }

    int m = 0;
    for (auto& s : S) {
        m = max(m, (int) s.length());
    }

    using PSI = pair<string,int>;

    map<PSI,vector<pair<PSI,int>>> graph;

    auto build = [&](string s, int d) {
        auto& V = graph[make_pair(s, d)];
        for (int j = 0; j < n; ++j) {
            string t = S[j];

            int sn = s.length();
            int tn = t.length();
            bool ok = true;
            string rem;
            int dir = 0;
            if (d == 0) {
                for (int i = 0; i < sn && i < tn; ++i) {
                    if (s[i] != t[tn - 1 - i]) {
                        ok = false;
                        break;
                    }
                }

                if (ok) {
                    if (sn >= tn) {
                        rem = s.substr(tn); 
                        dir = 0;
                    } else {
                        rem = t.substr(0, tn - sn);
                        dir = 1;
                    }
                }
            } else {
                for (int i = 0; i < sn && i < tn; ++i) {
                    if (s[sn - 1 - i] != t[i]) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    if (sn >= tn) {
                        rem = s.substr(0, sn - tn);
                        dir = 1;
                    } else {
                        rem = t.substr(sn);
                        dir = 0;
                    }
                }
            }

            if (ok) {
                V.emplace_back(make_pair(rem, dir), C[j]);
            }
        }
    };

    for (int i = 0; i < n; ++i) {
        string s = S[i];
        int m = s.length();

        for (int i = 0; i < m; ++i) {
            build(s.substr(i), 0);
        }
        for (int i = 0; i < m; ++i) {
            build(s.substr(0, i + 1), 1);
        }
    }


    map<pair<string,int>, long long> dis;

    using PIS = pair<long long, pair<string, int>>;
    priority_queue<PIS, vector<PIS>, greater<PIS>> pq;

    for (int i = 0; i < n; ++i) {
        auto u = make_pair(S[i], 0);
        pq.emplace(C[i], u);
        if (dis.count(u) == 0 || dis[u] > C[i]) {
            dis[u] = C[i];
        }
    }

    while (!pq.empty()) {
        auto u = pq.top().second;
        long long d = pq.top().first;
        pq.pop();
        if (dis[u] != d) {
            continue;
        }

        for (auto& e : graph[u]) {
            auto v = e.first;
            auto w = e.second;
            if (dis.count(v) == 0 || dis[v] > d + w) {
                dis[v] = d + w;
                pq.emplace(d + w, v);
            }
        }
    }


    auto isPalindrome = [&](string& s) {
        int n = s.length();
        for (int i = 0, j = n - 1; i < j; ++i, --j) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    };


    long long res = 1LL << 60;
    for (auto& P : dis) {
        string s = P.first.first;
        if (isPalindrome(s)) {
            res = min(res, P.second);
        }
    }

    if (res >= 1LL << 60) {
        res = -1;
    }

    cout << res << '\n';



    return 0;
}

Measured memory_bytes: 4140