# frozen_string_literal: true

n, m = gets.split.map(&:to_i)
uv = m.times.map { gets.split.map(&:to_i) }
s, t = gets.split.map(&:to_i)

d = Array.new(n + 1) { -1 }

ts = Array.new(n + 1) { [] }

uv.each do |e|
  u = e[0]
  v = e[1]
  ts[u].push v
end

$v0 = {}
$v1 = {}
$v2 = {}

def kenken(s, ts, d, count, dest)
  return if $v0[s]

  $v0[s] = true
  ts[s].each do |t1|
    next if $v1[t1]

    $v1[t1] = true
    ts[t1].each do |t2|
      next if $v2[t2]

      $v2[t2] = true
      ts[t2].each do |t3|
        if d[t3].negative?
          d[t3] = count
          dest.push t3
        end
      end
    end
  end
  dest
end

count = 1
dest = [s]
loop do
  break if dest.empty?

  ndest = []
  dest.each do |ss|
    kenken(ss, ts, d, count, ndest)
  end
  if d[t].positive?
    puts d[t]
    exit
  end
  count += 1
  dest = ndest
end
puts '-1'
