class Graph:
    class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

    def warshall_folyd(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

    def warshall_folyd2(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
        return dist

def max2(x,y):
    return x if x > y else y

from itertools import combinations

N, M, L = map(int, input().split())
graph = Graph(N, directed=False, decrement=True)
for _ in range(M):
    x, y, cost = map(int, input().split())
    graph.add_edge(x, y, cost)

data = []
d = graph.warshall_folyd2()

graph2 = Graph(N, directed=False, decrement=False)
for i, j in combinations(range(N), 2):
    if d[i][j] <= L:
        graph2.add_edge(i, j, 1)


d = graph2.warshall_folyd()

for _ in range(int(input())):
    s, t, = map(int, input().split())
    s, t = s-1, t-1
    print(d[s][t]-1 if d[s][t] < 1<<20 else -1) class Graph:
    class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

    def warshall_folyd(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

    def warshall_folyd2(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
        return dist Graph class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

     Edge def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

     __init__ """
            :param to:  終点ノード
            :param cost: 辺の重み
            """ self.to, self.cost = to, cost self.to self to to self.cost self cost cost self self to to cost cost def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

     __init__ self.n = n self.n self n n self.directed = directed self.directed self directed directed self.decrement = decrement self.decrement self decrement decrement [] for _ in range(self.n) for _ in range(self.n) _ range(self.n) range self.n self n for _ in range(self.n) [] self.edges = [[] for _ in range(self.n)] self.edges self edges [[] for _ in range(self.n)] self.edges2 = [] self.edges2 self edges2 [] self.parent = [-1]*self.n self.parent self parent [-1]*self.n [-1] -1 self.n self n self.info = [-1]*self.n self.info self info [-1]*self.n [-1] -1 self.n self n for x, y, cost in edges:
            self.add_edge(x, y, cost)

     x y cost edges self.add_edge(x, y, cost) self.add_edge self add_edge x y cost self self n n directed=False directed False decrement=True decrement True edges=[] edges [] def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

     add_edge if self.decrement:
            x -= 1
            y -= 1
         self.decrement self decrement x -= 1 x 1 y -= 1 y 1 self.edges[x].append(self.Edge(y, cost)) self.edges[x].append self.edges[x] self.edges self edges x append self.Edge(y, cost) self.Edge self Edge y cost self.edges2.append((x, y, cost)) self.edges2.append self.edges2 self edges2 append (x, y, cost) x y cost if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

     self.directed == False self.directed self directed False self.edges[y].append(self.Edge(x, cost)) self.edges[y].append self.edges[y] self.edges self edges y append self.Edge(x, cost) self.Edge self Edge x cost self.edges2.append((y, x, cost)) self.edges2.append self.edges2 self edges2 append (y, x, cost) y x cost self self x x y y cost cost def warshall_folyd(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

     warshall_folyd """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """ [INF] * self.n for _ in range(self.n) for _ in range(self.n) _ range(self.n) range self.n self n for _ in range(self.n) [INF] * self.n [INF] INF self.n self n dist = [[INF] * self.n for _ in range(self.n)] dist [[INF] * self.n for _ in range(self.n)] for i, j, cost in self.edges2:
            dist[i][j] = cost
         i j cost self.edges2 self edges2 dist[i][j] = cost dist[i][j] [i] dist i j cost for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

         i range(self.n) range self.n self n dist[i][i] = 0 dist[i][i] [i] dist i i 0 for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
         k range(self.n) range self.n self n for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
         i range(self.n) range self.n self n for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
         j range(self.n) range self.n self n if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
         dist[i][j] > dist[i][k] + dist[k][j] dist[i][j] [i] dist i j dist[i][k] + dist[k][j] dist[i][k] [i] dist i k dist[k][j] [k] dist k j dist[i][j] = dist[i][k] + dist[k][j] dist[i][j] [i] dist i j dist[i][k] + dist[k][j] dist[i][k] [i] dist i k dist[k][j] [k] dist k j return dist dist self self INF=1<<30 INF 1<<30 1 30 def warshall_folyd2(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
        return dist warshall_folyd2 """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """ [INF] * self.n for _ in range(self.n) for _ in range(self.n) _ range(self.n) range self.n self n for _ in range(self.n) [INF] * self.n [INF] INF self.n self n dist = [[INF] * self.n for _ in range(self.n)] dist [[INF] * self.n for _ in range(self.n)] for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
         i j cost self.edges2 self edges2 dist[i][j] = cost dist[i][j] [i] dist i j cost if dist[i][j] <= L:
                data.append((i, j))
         dist[i][j] <= L dist[i][j] [i] dist i j L data.append((i, j)) data.append data append (i, j) i j for i in range(self.n):
            dist[i][i] = 0

         i range(self.n) range self.n self n dist[i][i] = 0 dist[i][i] [i] dist i i 0 for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
         k range(self.n) range self.n self n for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
         i range(self.n) range self.n self n for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
         j range(self.n) range self.n self n if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
         dist[i][j] > dist[i][k] + dist[k][j] dist[i][j] [i] dist i j dist[i][k] + dist[k][j] dist[i][k] [i] dist i k dist[k][j] [k] dist k j dist[i][j] = dist[i][k] + dist[k][j] dist[i][j] [i] dist i j dist[i][k] + dist[k][j] dist[i][k] [i] dist i k dist[k][j] [k] dist k j if dist[i][j] <= L:
                            data.append((i, j))
         dist[i][j] <= L dist[i][j] [i] dist i j L data.append((i, j)) data.append data append (i, j) i j return dist dist self self INF=1<<30 INF 1<<30 1 30 def max2(x,y):
    return x if x > y else y max2 return x if x > y else y x if x > y else y x > y x y x y x x y y from itertools import combinations N, M, L = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M L graph = Graph(N, directed=False, decrement=True) graph Graph(N, directed=False, decrement=True) Graph N directed=False directed False decrement=True decrement True for _ in range(M):
    x, y, cost = map(int, input().split())
    graph.add_edge(x, y, cost) _ range(M) range M x, y, cost = map(int, input().split()) x map(int, input().split()) map int input().split() ().split () input split y cost graph.add_edge(x, y, cost) graph.add_edge graph add_edge x y cost data = [] data [] d = graph.warshall_folyd2() d graph.warshall_folyd2() graph.warshall_folyd2 graph warshall_folyd2 graph2 = Graph(N, directed=False, decrement=False) graph2 Graph(N, directed=False, decrement=False) Graph N directed=False directed False decrement=False decrement False for i, j in combinations(range(N), 2):
    if d[i][j] <= L:
        graph2.add_edge(i, j, 1) i j combinations(range(N), 2) combinations range(N) range N 2 if d[i][j] <= L:
        graph2.add_edge(i, j, 1) d[i][j] <= L d[i][j] [i] d i j L graph2.add_edge(i, j, 1) graph2.add_edge graph2 add_edge i j 1 d = graph2.warshall_folyd() d graph2.warshall_folyd() graph2.warshall_folyd graph2 warshall_folyd for _ in range(int(input())):
    s, t, = map(int, input().split())
    s, t = s-1, t-1
    print(d[s][t]-1 if d[s][t] < 1<<20 else -1) _ range(int(input())) range int(input()) int input() input s, t, = map(int, input().split()) s map(int, input().split()) map int input().split() ().split () input split t s, t = s-1, t-1 s s-1 s 1 t t-1 t 1 print(d[s][t]-1 if d[s][t] < 1<<20 else -1) print d[s][t]-1 if d[s][t] < 1<<20 else -1 d[s][t] < 1<<20 d[s][t] [s] d s t 1<<20 1 20 d[s][t]-1 d[s][t] [s] d s t 1 -1 x, y, cost = map(int, input().split()) map(int, input().split()) x d = graph2.warshall_folyd() graph2.warshall_folyd() d M, L = map(int, input().split()) map(int, input().split()) M graph2 = Graph(N, directed=False, decrement=False) Graph(N, directed=False, decrement=False) graph2 s, t = s-1, t-1 s-1 s N, M, L = map(int, input().split()) map(int, input().split()) N graph = Graph(N, directed=False, decrement=True) Graph(N, directed=False, decrement=True) graph t, = map(int, input().split()) map(int, input().split()) t data = [] [] data L = map(int, input().split()) map(int, input().split()) L class Graph:
    class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

    def warshall_folyd(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

    def warshall_folyd2(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
        return dist class Graph:
    class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

    def warshall_folyd(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

    def warshall_folyd2(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
        return dist Graph t = s-1, t-1 t-1 t def max2(x,y):
    return x if x > y else y def max2(x,y):
    return x if x > y else y max2 s, t, = map(int, input().split()) map(int, input().split()) s cost = map(int, input().split()) map(int, input().split()) cost y, cost = map(int, input().split()) map(int, input().split()) y d = graph.warshall_folyd2() graph.warshall_folyd2() d
class Graph:
    class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

    def warshall_folyd(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

    def warshall_folyd2(self, INF=1<<30):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
        return dist

def max2(x,y):
    return x if x > y else y

from itertools import combinations

N, M, L = map(int, input().split())
graph = Graph(N, directed=False, decrement=True)
for _ in range(M):
    x, y, cost = map(int, input().split())
    graph.add_edge(x, y, cost)

data = []
d = graph.warshall_folyd2()

graph2 = Graph(N, directed=False, decrement=False)
for i, j in combinations(range(N), 2):
    if d[i][j] <= L:
        graph2.add_edge(i, j, 1)


d = graph2.warshall_folyd()

for _ in range(int(input())):
    s, t, = map(int, input().split())
    s, t = s-1, t-1
    print(d[s][t]-1 if d[s][t] < 1<<20 else -1)