#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
using namespace         std;
#define                 int long long
#define                 pb push_back
#define                 mp make_pair
#define st(arr,a)       memset(arr,a,sizeof arr)
#define                 nl cout<<'\n'
#define bitcount(x)     __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i,a,b)     for(int i=a;i<=b;i++)
#define rfor(i,a,b)     for(int i=a;i>=b;i--)
#define all(x)          x.begin() , x.end()
#define pi pair<int,int>
#define X first
#define Y second
#define N 1014159
#define  vi vector<int>
#define  v  vector
#define die exit(0)
#define alln(arr) arr+1 , arr+1+n
using namespace __gnu_pbds;  
typedef tree<int, null_type, less<int>, rb_tree_tag,  
            tree_order_statistics_node_update> oset; 

// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).

#define in input_int()
#define ins input_string()
int input_int(){int a;cin>>a;return a;}
string input_string(){string s;cin>>s;return s;}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};



int n,m,k,a,b,c,d,x,y,arr[N];
string s[N];

int dp[55][55][55][2];

string p;

bool get_right(string a,string b)
{
    p.clear();
    reverse(all(a));

    while(a.size() and b.size())
    {
        if(a.back()==b.back())
        {
            p+=a.back();
            a.pop_back();
            b.pop_back();

        }
        else return 0;
    }
    return 1;

}

bool get_left(string a,string b)
{
    reverse(all(b));
    p.clear();

    while(a.size() and b.size())
    {
        if(a.back()==b.back())
        {
            p+=a.back();
            a.pop_back();
            b.pop_back();

        }
        else return 0;
    }
    return 1;

}

bool pallin(string a)
{
    string b=a;
    reverse(all(a));
    return a==b;
}


int get(int idx,int i,int j,int f)
{
    if(j==i or j<i)return 0;
    int&ans=dp[idx][i][j][f];
    if(ans!=-1)return ans;
    string t;
    forn(k,i,j)t+=s[idx][k];
    ans=MOD*MOD;

    if(pallin(t))
    {
        ans=0;
    }
    else
    {
        forn(k,1,n)
        {
            if(f)
            {
                if(get_left( t, s[k]))
                {
                    
                    if(p.size()==t.size())
                    {
                        ans=min(arr[k]+get(k,p.size(),s[k].size()-1,0),ans);
                    }
                    else
                    {
                        ans=min(ans,arr[k]+get(idx,i,j-p.size(),1));
                    }
                }
            }
            else
            {
                if(get_right(t,s[k]))
                {
                    
                    if(p.size()==t.size())
                    {
                        ans=min(arr[k]+get(k,0,s[k].size()-1-p.size(),1),ans);
                    }
                    else
                    {
                        ans=min(ans,arr[k]+get(idx,i+p.size(),j,0));
                    }
                }
            }
        }
    }
    return ans;

}

void solve()
{
    cin>>n;
    forn(i,1,n)cin>>s[i]>>arr[i];
    st(dp,-1);
    int ans=MOD*MOD;
    forn(i,1,n)if(pallin(s[i]))ans=min(ans,arr[i]);
    forn(i,1,n)
    {
        ans=min(ans,arr[i]+get(i,0,s[i].size()-1,1));
    }
    if(ans>1e15)ans=-1;
    cout<<ans;

}


signed main() 
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 
    int t = 1;
    //cin>>t;
    forn(i,1,t)
    {
        solve();
        nl;
    }



    return 0;

}
//       clang++ c.cpp ; ./a.out < a.txt ;  

Measured memory_bytes: 37964