# frozen_string_literal: true in_n, in_m = gets.chomp.split.map(&:to_i) in_uv = in_m.times.map { gets.chomp.split.map(&:to_i) } in_k = gets.chomp.to_i in_a = if in_k > 0 gets.chomp.split.map(&:to_i).to_set else Set[] end edges = Hash.new { |h, k| h[k] = Set.new } in_uv.each do |u, v| 5.times do |i| shift = in_n * i if in_a.include?(v) edges[u + shift] << v + shift + in_n if i < 4 else edges[u + shift] << v end if in_a.include?(u) edges[v + shift] << u + shift + in_n if i < 4 else edges[v + shift] << u end end end queue = [1] distances = {} distances[1] = 0 until queue.empty? q = queue.shift edges[q].each do |e| if distances[e].nil? distances[e] = distances[q] + 1 queue << e end end end puts (5.times.map { |i| distances[in_n + in_n * i] }.compact.min || -1)