#define DIN freopen("input.txt","r",stdin);
#define DOUT freopen("output.txt","w",stdout);
#include <bits/stdc++.h>
#include <cstdio>
#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=(a);i<=(int)(b);i++)
#define REP_(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef std::vector<int> VI;
typedef std::pair<int,int> PI;
int read()
{
    int x=0,flag=1; char c=getchar();
    while((c>'9' || c<'0') && c!='-') c=getchar();
    if(c=='-') flag=0,c=getchar();
    while(c<='9' && c>='0') {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return flag?x:-x;
}

const int maxn=1e5+5;
typedef pair<string,int> psi;
map<psi,int> t;
struct edge {int v; LL w;};
vector<edge> G[maxn];
int n,m,c[maxn];
string s[maxn];

void add(int u,int v,int w)
{
    G[u].pb((edge){v,w});
}

bool huiwen(string v)
{
    if(v.length()<=1) return 1;
    int l=0,r=v.length()-1;
    while(l<r)
    {
        if(v[l]!=v[r]) return 0;
        l++,r--;
    }
    return 1;
}

int peidui(string left,string right,string &ret)
{
    int l=left.length(),r=right.length();
    int i=0,j=r-1;
    while(i<l && j>=0)
    {
        if(left[i]!=right[j]) return -1;
        i++; j--;
    }
    if(i>=l && j<0) {ret=""; return 0;}
    else if(j>=0) {ret=right.substr(0,j+1); return 1;}
    else if(i<l) {ret=left.substr(i,l); return 0;}
}

const LL inf=1e15+1;
LL d[maxn],vis[maxn];

void dijkstra(int s)
{
    fill(d,d+n+5,inf);
    //mem(vis,0);
    d[s]=0;
    typedef pair<LL,int> P;
    priority_queue<P,vector<P>,greater<P> > Q;
    Q.push(P(0,s));
    while(!Q.empty())
    {
        P p=Q.top(); Q.pop();
        LL dis=p.first,u=p.second;
        if(vis[u]) continue;
        d[u]=dis; vis[u]=1;
        REP(i,0,G[u].size()-1)
        {
            int v=G[u][i].v,w=G[u][i].w;
            if(vis[v] || dis+w>d[v]) continue;
            Q.push(P(d[v]=dis+w,v));
        }
    }
}

void print()
{
    for(map<psi,int>::iterator i=t.begin();i!=t.end();i++)
    {
        string v=i->first.first;
        int type=i->first.second;
        int id=i->second;
        cout<<v<<' '<<type<<' '<<id<<":   \n";
        for(edge e:G[id]) cout<<e.v<<' '<<e.w<<endl;
        cout<<endl;
    }
}

int main()
{
    m=read();
    REP(i,1,m)
    {
        cin>>s[i]>>c[i];
        int len=s[i].length();
        //cout<<s[i]<<' '<<len<<endl;
        for(int j=len-1;j>=0;j--)
            t[psi(s[i].substr(j,len),0)]=++n;
        for(int j=1;j<=len;j++)
            t[psi(s[i].substr(0,j),1)]=++n;
    }
    t[psi("",0)]=++n;
    t[psi("",1)]=++n;
    REP(i,1,m)
    {
        add(n+1,t[psi(s[i],0)],c[i]);
        add(n+1,t[psi(s[i],1)],c[i]);
    }
    for(map<psi,int>::iterator i=t.begin();i!=t.end();i++)
    {
        if(huiwen(i->first.first)) add(i->second,n+2,0);
    }

    for(map<psi,int>::iterator i=t.begin();i!=t.end();i++)
    {
        psi p=i->first;
        int id=i->second;
        for(int i=1;i<=m;i++)
        {
            string v;
            int ret;
            if(p.second==0) ret=peidui(p.first,s[i],v);
            else ret=peidui(s[i],p.first,v);
            if(ret!=-1)
            {
                add(t[p],t[psi(v,ret)],c[i]);
            }
        }
    }

    //print();

    dijkstra(n+1);
    if(d[n+2]>=inf) cout<<"-1";
    else cout<<d[n+2];

    return 0;
}


Measured memory_bytes: 9328