#include<iostream>
#include<unordered_map>
#include<map>
#include<vector>
#include<set>
#define INF 0x3f3f3f3f
using namespace std;
int costs[10];
int n;
vector<int>result;
map<long long, int>names;
struct Point
{
Point(int i) {
distence = INF;
done = false;
index = i;
}
long long name;
Point* parent;
long long distence;
bool done;
int index;
};
unordered_map<Point*, int>* neighbor;
vector<Point>point;
void cal_diff(Point* pointNow) {
delete neighbor;
neighbor = new unordered_map<Point*, int>;
map<long long, int>::iterator ptr;
long long name = pointNow->name;
long long temp;
for (int i = 0; i < 10; i++) {
temp = name - name % long long(pow(10, i + 1)) / long long(pow(10, i)) * long long(pow(10, i)) - long long(pow(10, i));;
for (int j = 0; j < 10; j++) {
temp += long long(pow(10, i));
if (temp == name)continue;
ptr = names.find(temp);
if (ptr != names.end()) (*neighbor).insert({ &point[ptr->second], costs[9 - i] });
}
}
for (int i = 0; i < 10; i++) {
int a = name % long long(pow(10, i + 1)) / long long(pow(10, i));
for (int j = i + 1; j < 10; j++) {
int b = name % long long(pow(10, j + 1)) / long long(pow(10, j));
temp = name + long long(pow(10, i)) * (b - a) + long long(pow(10, j)) * (a - b);
ptr = names.find(temp);
if (ptr != names.end()) (*neighbor).insert({ &point[ptr->second], costs[9 - j] });
}
}
}
void dijkstra()
{
set<pair<int, Point*>>queue;
point[0].distence = 0;
queue.insert(make_pair(0, &point[0]));
Point* now;
unordered_map<Point*, int>::iterator ptr;
while (!queue.empty())
{
now = queue.begin()->second;
if (now->index == n) return;
queue.erase(queue.begin());
if (now->done)
continue;
cal_diff(now);
now->done = true;
for (ptr = neighbor->begin(); ptr != neighbor->end(); ptr++) {
Point* n = ptr->first;
int cost = ptr->second;
if (!n->done && n->distence > now->distence + cost) {
n->parent = now;
n->distence = now->distence + cost;
queue.insert(make_pair(n->distence, n));
}
}
}
}
void chaseBack() {
result.push_back(n);
Point* pointTemp = point[n - 1].parent;
while (pointTemp != &point[0]) {
result.insert(result.begin(), pointTemp->index);
pointTemp = pointTemp->parent;
}
result.insert(result.begin(), 1);
}
int main() {
cin >> n;
for (int i = 0; i < 10; i++)cin >> costs[i];
for (int i = 0; i < n; i++) {
point.push_back(*new Point(i + 1));
cin >> point[i].name;
names.insert({ point[i].name, i });
}
dijkstra();
if (point[n - 1].distence >= INF) cout << -1 << endl;
else {
chaseBack();
cout << point[n - 1].distence << endl;
cout << result.size() << endl;
for (auto i : result)cout << i << " ";
}
}