program main implicit none integer n,m integer p,q integer i read(*,*)n,m do i=1,m read(*,*)p,q if(p==n)n=q enddo write(*,*)n end program main