#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

int main()
{
    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];

    function<long long (int, int)> fL, fR;

    long long oo = 1000000000000000000LL;

    vector<vector<long long>> ML(N, vector<long long>(21, -1));
    fL = [&](int si, int l) -> long long
    {
        if (ML[si][l]>=0)
            return ML[si][l];
        if (ML[si][l]==-2)
            return oo;
        ML[si][l] = -2;

        bool ok = true;
        for (int i=0; i<l && ok; i++)
            if (S[si][S[si].size()-l+i]!=S[si][S[si].size()-i-1])
                ok = false;
        if (ok)
            return ML[si][l]=0;

        long long ans = oo;
        for (int i=0; i<N; i++)
        {
            bool ok = true;
            int t = min(l, (int)S[i].size());
            for (int j=0; j<t && ok; j++)
                if (S[si][S[si].size()-l+j]!=S[i][S[i].size()-j-1])
                    ok = false;
            if (ok)
            {
                long long a = 0;
                if (l<(int)S[i].size())
                    a = fR(i, S[i].size()-l);
                else
                    a = fL(si, l-S[i].size());
                ans = min(ans, a+C[i]);
            }
        }
        return ML[si][l] = ans;
    };

    vector<vector<long long>> MR(N, vector<long long>(21, -1));
    fR = [&](int si, int l) -> long long
    {
        if (MR[si][l]>=0)
            return MR[si][l];
        if (MR[si][l]==-2)
            return oo;
        MR[si][l] = -2;

        bool ok = true;
        for (int i=0; i<l && ok; i++)
            if (S[si][i]!=S[si][l-i-1])
                ok = false;
        if (ok)
            return MR[si][l]=0;

        long long ans = oo;
        for (int i=0; i<N; i++)
        {
            bool ok = true;
            int t = min((int)S[i].size(), l);
            for (int j=0; j<t && ok; j++)
                if (S[i][j]!=S[si][l-j-1])
                    ok = false;
            if (ok)
            {
                long long a = 0;
                if (l<(int)S[i].size())
                    a = fL(i, S[i].size()-l);
                else
                    a = fR(si, l-S[i].size());
                ans = min(ans, a+C[i]);
            }
        }
        return MR[si][l] = ans;
    };

    long long ans = oo;
    for (int i=0; i<N; i++)
        ans = min(ans, fL(i, (int)S[i].size())+C[i]);
    cout<<(ans<oo ? ans : -1)<<endl;
}
