1 (*
2 *Problem: 假面舞会
3 *Author : Chen Yang
4 *Time : 2012.5.18
5 *State : Solved
6 *Memo : 图论、有向图找环
7 *)
8 program party;
9 const maxn=101000;
10 type
11 ty1=^ty2;
12 ty2=record
13 x,d:longint;
14 get:boolean;
15 next,up:ty1;
16 end;
17
18 var
19 n,m,i,a,b,min,max,cnt,tot:longint;
20 first:array[0..maxn] of ty1;
21 get:array[0..maxn] of boolean;
22 link,c,bel,cmin,cmax:array[0..maxn] of longint;
23 //==================
24 procedure insert(x,y,z:longint);
25 var
26 p,q:ty1;
27 begin
28 new(p);
29 p^.x:=y; p^.d:=z; p^.get:=false;
30 p^.next:=first[x]; first[x]:=p;
31 new(q);
32 q^.x:=x; q^.d:=-z; q^.get:=false;
33 q^.next:=first[y]; first[y]:=q;
34 q^.up:=p; p^.up:=q;
35 end;
36 //==================
37 procedure find(x,i:longint);
38 var
39 p:ty1;
40 begin
41 get[x]:=true; link[x]:=i;
42 bel[x]:=tot;
43 p:=first[x];
44 while p<>nil do
45 begin
46 if not p^.get then
47 begin
48 p^.get:=true; p^.up^.get:=true;
49 if not get[p^.x] then find(p^.x,i+p^.d) else
50 if link[p^.x]-i-p^.d<>0 then
51 begin
52 inc(cnt); c[cnt]:=abs(link[p^.x]-i-p^.d);
53 end;
54 end;
55 p:=p^.next;
56 end;
57 end;
58 //==================
59 function gcd(x,y:longint):longint;
60 begin if y=0 then exit(x) else exit(gcd(y,x mod y)); end;
61 //==================
62 procedure main1;
63 var
64 i:longint;
65 begin
66 max:=c[1];
67 for i:=2 to cnt do max:=gcd(max,c[i]);
68 for min:=3 to max do
69 begin
70 for i:=1 to cnt+1 do
71 if c[i] mod min<>0 then break;
72 if i=cnt+1 then break;
73 end;
74 if max<3 then
75 begin
76 min:=-1; max:=-1;
77 end;
78 end;
79 //==================
80 procedure main2;
81 var
82 i:longint;
83 begin
84 fillchar(cmin,sizeof(cmin),$7);
85 fillchar(cmax,sizeof(cmax),200);
86 for i:=1 to n do
87 begin
88 if cmin[bel[i]]>link[i] then cmin[bel[i]]:=link[i];
89 if cmax[bel[i]]<link[i] then cmax[bel[i]]:=link[i];
90 end;
91 for i:=1 to tot do inc(max,cmax[i]-cmin[i]+1);
92 if max>2 then min:=3 else
93 begin
94 min:=-1; max:=-1;
95 end;
96 end;
97 //==================
98 begin
99 assign(input,'party.in'); reset(input);
100 assign(output,'party.out'); rewrite(output);
101 read(n,m);
102 for i:=1 to m do
103 begin
104 read(a,b); insert(a,b,1);
105 end;
106 for i:=1 to n do
107 if not get[i] then
108 begin inc(tot); find(i,1); end;
109 if cnt>0 then main1 else main2;
110 writeln(max,' ',min);
111 close(input); close(output);
112 end.