#include <bits/stdc++.h>
#include <iostream>
#include <ctime>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

bool checkinit1(string &t, int j, int l)
{
    rep(k, j)
    {
        if( j + k + 1 >= l ) break;
        if( t[ j + k + 1] != t[j - k - 1] ) return false; 
    }
    return true;
}

bool checkinit2(string &t, int j, int l)
{
    rep(k, j)
    {
        if( j + k >= l ) break;
        if( t[ j + k ] != t[j - k - 1] ) return false; 
    }
    return true;
}


int main()
{
    int n;
    cin >> n;
    vector<string> s;
    _LL *c = new _LL[n];

    rep(i, n)
    {
        string st;
        cin >> st >> c[i];
        s.push_back(st);
    }

    map<string, _LL> m;
    priority_queue<pair<_LL, string>> q;

    rep(i, n)
    {
        int l = s[i].length();
        rep(j, l)
        {
            if( checkinit1(s[i], j, l) )
            {
                string tmp = "";
                if( j == l - j - 1 )
                {
                    tmp = "";
                }
                else if ( j < l - j - 1 )
                {
                    tmp = "0";
                    for( int k = j + j + 1; k < l; k++ ) tmp += s[i][k];
                }
                else
                {
                    tmp = "1";
                    for( int k = j + j - l; k >= 0; k-- ) tmp += s[i][k];
                }
                if( m[tmp] == 0 || m[tmp] > c[i] ) 
                {
                    m[tmp] = c[i];
                    q.push(make_pair(-c[i], tmp));
                }
            }

            if( checkinit2(s[i], j, l) )
            {
                string tmp = "";
                if( j == l - j  )
                {
                    tmp = "";
                }
                else if ( j < l - j  )
                {
                    tmp = "0";
                    for( int k = j + j; k < l; k++ ) tmp += s[i][k];
                }
                else
                {
                    tmp = "1";
                    for( int k = j + j - l - 1; k >= 0; k-- ) tmp += s[i][k];
                }
                if( m[tmp] == 0 || m[tmp] > c[i] ) 
                {
                    m[tmp] = c[i];
                    q.push(make_pair(-c[i], tmp));
                }
            }
        }
    }

    while( !q.empty() )
    {
        auto next = q.top();
        q.pop();
//        cout << next.second << "," << next.first << endl;
        if( m[next.second] < next.first ) continue;
        if( next.second == "" )
        {
            cout << -next.first;
            return 0;
        }

        string t = next.second;
        if( t[0] == '0' )
        {
            // 必要があればTrieなどで高速化。まずは間に合うか実装
            rep(i, n)
            {
                _LL cost = -next.first + c[i];
                int k = 0;
                bool flag = true;
                int l = s[i].length();
                while( k + 1 < t.length() && k < l )
                {
                    if( t[k + 1] != s[i][l - k - 1] )
                    {
                        flag = false;
                        break;
                    }
                    k++;
                }
                if( flag )
                {
                    string tmp = "";
                    if( k == l )
                    {
                        if( k + 1 < t.length() )
                        {
                            tmp = "0";
                            for( ; k + 1 < t.length(); k++ ) tmp += t[k + 1];
                        }
                    }
                    else
                    {
                        tmp = "1";
                        for(; k < l; k++ ) tmp += s[i][l - k - 1];
                    }
                    if( m[tmp] == 0 || m[tmp] > cost ) 
                    {
                        m[tmp] = cost;
                        q.push(make_pair(-cost, tmp));
                    }
                }
            }
        }
        else if( t[0] == '1' )
        {
            rep(i, n)
            {
                _LL cost = -next.first + c[i];
                int k = 0;
                bool flag = true;
                int l = s[i].length();
                while( k + 1 < t.length() && k < l )
                {
                    if( t[k + 1] != s[i][k] )
                    {
                        flag = false;
                        break;
                    }
                    k++;
                }
                if( flag )
                {
                    string tmp = "";
                    if( k == l )
                    {
                        if( k + 1 < t.length() )
                        {
                            tmp = "1";
                            for( ; k + 1 < t.length(); k++ ) tmp += t[k + 1];
                        }
                    }
                    else
                    {
                        tmp = "0";
                        for(; k < l; k++ ) tmp += s[i][k];
                    }
                    if( m[tmp] == 0 || m[tmp] > cost ) 
                    {
                        m[tmp] = cost;
                        q.push(make_pair(-cost, tmp));
                    }
                }
            }
        }
    }

    delete c;
    cout << -1;
}

Measured memory_bytes: 3668