import std.stdio, std.conv, std.string, std.range, std.math, std.algorithm; void main() { auto N = readln.strip.to!int; auto M = readln.strip.to!int; int[] P, Q; foreach (_; 0 .. M) { auto input = readln.split.to!(int[]); P ~= input[0]; Q ~= input[1]; } auto cup = [0, 1, 2, 3]; iota(M).each!(i=>swap(cup[P[i]],cup[Q[i]])); cup.countUntil(N).writeln; }