require 'set'
require 'prime'

n, m, s, t = gets.split.map(&:to_i)
g = Hash[(1..n).map {|i| [i, []] }]
m.times do
  i, j = gets.split.map(&:to_i)
  g[i] << j
  g[j] << i
end

if g[s].any? {|i| g[i].include?(t) }
  p 2
  exit
end

dist = { t => 0 }
color = { t => 0 }
queue = [t]
has_odd_cycle = false
until queue.empty?
  i = queue.shift
  g[i].each do |j|
    if color[j] && (color[j] == color[i])
      has_odd_cycle = true
    end
    next if dist[j]
    dist[j] = dist[i] + 1
    color[j] = 1 - color[i]
    queue << j
  end
end

if Prime.prime?(dist[s])
  p dist[s]
elsif dist[s].odd?
  d = dist[s]
  d += 2 until Prime.prime?(d)
  p d
elsif !has_odd_cycle
  p -1
else
  dist2 = { s => 0 }
  queue = [s]
  d = nil
  until queue.empty?
    i = queue.shift
    g[i].each do |j|
      d = dist2[i] + 1 + dist[j]
      if d.odd?
        queue = []
        break
      end
      next if dist2[j]
      dist2[j] = dist2[i] + 1
      queue << j
    end
  end
  d += 2 until Prime.prime?(d)
  p d
end

