#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
//int g[20000][2000], used[20000], dist[20000];
vector<int> dist;
vector<bool> used;
vector <int> p;
void relax(int i, int j, int h){
if (dist[i] + h < dist[j]) {
dist[j] = dist[i] + h;
p[j] = i;
}
}
int main() {
int n, s, f, v, min;
cin >> n >> s >> f;
--s;
//--f;
int g[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> g[i][j];
}
}
used.resize(n, 0);
dist.resize(n, INT_MAX);
p.resize(n);
dist[s] = 0;
for(int i = 0; i < n; i++) {
min = INT_MAX;
v = -1;
for (int j = 0; j < n; j++) {
if (used[j] == 0 && dist[j] < min) {
min = dist[j];
v = j;
}
}
if (v < 0) break;
for (int j = 0; j < n; j++)
if (used[j] == 0 && g[v][j] != -1) {
relax(v, j, g[v][j]);
p[j] = v;
}
used[v] = 1;
}
if (dist[f] == INT_MAX) {
cout << "-1";
return 0;
}
cout << dist[--f] << endl;
vector<int> path;
for (int v = f; v != s; v = p[v])
path.push_back(v);
path.push_back (s);
reverse(path.begin(), path.end());
for(auto elem : path)
cout << elem << ' ';
}