using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DejkstraWithStrangeStructure
{
class Program
{
static void Main(string[] args)
{
//StreamReader sr = new StreamReader("input.txt");
//StreamWriter sw = new StreamWriter("output.txt");
Func<string> getLine = Console.ReadLine;
Action<string> putLine = (string p) => Console.WriteLine(p);
Int32[] tmp; char[] seps = new char[] { ' ' };
tmp = getLine().Split(seps, StringSplitOptions.RemoveEmptyEntries).Select(p => Int32.Parse(p)).ToArray();
Int32 n = tmp[0];
//Int32 m = tmp[1];
Int32 start = tmp[1] - 1;
Int32 finish = tmp[2] - 1;
List<List<Tuple<Int32, Int32>>> g = new List<List<Tuple<Int32, Int32>>>(n);
for (Int32 i = 0; i < n; ++i)
g.Add(new List<Tuple<Int32, Int32> >());
//for (Int32 i = 0; i < m; ++i)
//{
// tmp = getLine().Split().Select(p => Int32.Parse(p)).ToArray();
// Int32 a, b, w;
// a = tmp[0] - 1;
// b = tmp[1] - 1;
// w = tmp[2];
// g[a].Add(new Tuple<Int32, Int32>(b, w));
// g[b].Add(new Tuple<Int32, Int32>(a, w));
//}
for (Int32 i = 0; i < n; ++i)
{
tmp = getLine().Split(seps, StringSplitOptions.RemoveEmptyEntries).Select(p => Int32.Parse(p)).ToArray();
for (Int32 j = 0; j < n; ++j)
if (tmp[j] >= 0 && i != j)
g[i].Add(new Tuple<Int32, Int32>(j, tmp[j]));
}
Int32[] dist;
Int32[] pred;
Dejkstra(g, start, out dist, out pred);
if (dist[finish] == Int32.MaxValue) dist[finish] = -1;
//if (dist[finish] >= Int32.MaxValue / 2) throw new Exception("=(");
putLine(dist[finish].ToString());
}
//Tuple<Int32, Int32> - pair destination/weight
public static void Dejkstra(List<List<Tuple<Int32, Int32> > > g, Int32 start, out Int32[] dist, out Int32[] pred)
{
Int32 n = g.Count;
Int32 maxDist = 0;
for (Int32 i = 0; i < n; ++i)
foreach (Tuple<Int32, Int32> p in g[i])
maxDist = Math.Max(maxDist, p.Item2);
dist = new Int32[n];
pred = new Int32[n];
for (Int32 i = 0; i < n; ++i)
{
dist[i] = Int32.MaxValue;
pred[i] = -1;
}
dist[start] = 0;
int cherpacscnt = 10 * maxDist + 1;
StrangeClass cherpaks = new StrangeClass(n, cherpacscnt);
cherpaks.PutNodeToCherpackEnd(start, 0);
Int32 curCherpack = 0;
Int32 zeroIters = 0;
while (zeroIters <= maxDist + 1)
{
if (cherpaks.cherpaksFirst[curCherpack] == null)
{
++zeroIters;
}
else
{
StrangeClass.Node cur = cherpaks.cherpaksFirst[curCherpack];
while (cur != null)
{
Int32 v = cur.vertex;
cherpaks.EraseVertexFromCherpak(v);
foreach(Tuple<Int32, Int32> p in g[v])
{
Int32 u = p.Item1;
Int32 w = p.Item2;
if (dist[v] + w < dist[u])
{
pred[u] = v;
dist[u] = dist[v] + w;
if (dist[u] >= Int32.MaxValue / 2) throw new Exception("=(");
cherpaks.PutNodeToCherpackEnd(u, dist[u]);
}
}
cur = cherpaks.cherpaksFirst[curCherpack];
}
zeroIters = 0;
}
curCherpack = (curCherpack + 1) % cherpacscnt;
}
}
}
class StrangeClass
{
public readonly Int32 vertexCount;
public readonly Int32 cherpaksCount;
public readonly Node[] nodes;
public readonly Node[] cherpaksFirst;
public readonly Node[] cherpaksLast;
private readonly Int32[] nodeIdToCherpakId;
public void EraseVertexFromCherpak(Int32 vertex)
{
Int32 chId = nodeIdToCherpakId[vertex];
if (chId < 0) return;
if (nodes[vertex].next == null)
{
cherpaksLast[chId] = nodes[vertex].prev;
}
else
{
nodes[vertex].next.prev = nodes[vertex].prev;
}
if (nodes[vertex].prev == null)
{
cherpaksFirst[chId] = nodes[vertex].next;
}
else
{
nodes[vertex].prev.next = nodes[vertex].next;
}
nodeIdToCherpakId[vertex] = -1;
nodes[vertex].next = null;
nodes[vertex].prev = null;
}
public void PutNodeToCherpackEnd(Int32 vertex, Int32 cherpack)
{
EraseVertexFromCherpak(vertex);
cherpack %= cherpaksCount;
nodes[vertex].next = null;
nodes[vertex].prev = cherpaksLast[cherpack];
if (nodes[vertex].prev == null)
cherpaksFirst[cherpack] = nodes[vertex];
else
nodes[vertex].prev.next = nodes[vertex];
cherpaksLast[cherpack] = nodes[vertex];
nodeIdToCherpakId[vertex] = cherpack;
}
public StrangeClass(Int32 vertexCount, Int32 cherpaksCount)
{
this.cherpaksCount = cherpaksCount;
this.vertexCount = vertexCount;
nodes = new Node[vertexCount];
nodeIdToCherpakId = new Int32[vertexCount];
cherpaksFirst = new Node[cherpaksCount];
cherpaksLast = new Node[cherpaksCount];
for (Int32 i = 0; i < vertexCount; ++i)
{
nodes[i] = new Node(i);
nodeIdToCherpakId[i] = -1;
}
for (Int32 i = 0; i < cherpaksCount; ++i)
{
cherpaksFirst[i] = cherpaksLast[i] = null;
}
}
public class Node
{
public Node next;
public Node prev;
public readonly Int32 vertex;
public Node(Int32 vertex)
{
next = null;
prev = null;
this.vertex = vertex;
}
}
}
}