1 #include"stdio.h"
2 #include"string.h"
3 const int ms=100004;
4 int left[100004];
5 int right[100004];
6 int *tmp1,*tmp2;
7 inline void link(int a,int b)
8 {
9 right[a]=b;
10 left[b]=a;
11 }
12 int main()
13 {
14 int n,m,i,j,k,flag,x,y,p=1,cnt,next;
15 while(scanf("%d%d",&n,&m)!=EOF)
16 {
17 for(i=1;i<=n;i++)
18 {
19 left[i]=i-1;
20 right[i]=i+1;
21 }
22 right[n]=0;//构成环,方便寻找开始节点和结束节点
23 cnt=0;
24 while(m--)
25 {
26 scanf("%d",&flag);
27 if(flag!=4)
28 scanf("%d%d",&x,&y);
29 else
30 cnt++;
31 if((cnt&1)&&flag<3)
32 flag=3-flag;
33 if(flag==1&&(right[x]!=y))
34 {
35 if(right[y]==x)
36 {
37 int t1=left[x],t2=right[x],t3=left[y],t4=right[y];
38 link(t3,x);
39 link(x,y);
40 link(y,t2);
41 }
42 else
43 {
44 link(left[x],right[x]);
45 link(left[y],x);
46 link(x,y);
47 }
48 }
49 else if(flag==2&&right[y]!=x)
50 {
51 if(right[x]==y)
52 {
53 int t1=left[x],t2=right[x],t3=left[y],t4=right[y];
54 link(t1,y);
55 link(y,x);
56 link(x,t4);
57 }
58 else
59 {
60 link(left[x],right[x]);
61 link(x,right[y]);
62 link(y,x);
63 }
64 }
65 else if(flag==3)
66 {
67 if(right[x]==y)
68 {
69 int t1=left[x],t2=right[x],t3=left[y],t4=right[y];
70 link(t1,y);
71 link(y,x);
72 link(x,t4);
73 }
74 else if(right[y]==x)
75 {
76 int t1=left[x],t2=right[x],t3=left[y],t4=right[y];
77 link(t3,x);
78 link(x,y);
79 link(y,t2);
80 }
81 else{
82 int t1=left[x],t2=right[x],t3=left[y],t4=right[y];
83 link(t1,y);
84 link(y,t2);
85 link(t3,x);
86 link(x,t4);
87 }
88 }
89 }
90 long long ans=0;
91 if(cnt&1)
92 {
93 tmp2=right;
94 tmp1=left;
95 }
96 else
97 {
98 tmp2=left;
99 tmp1=right;
100 }
101 for(i=1;i<=n;i++)
102 {
103 if(tmp2[i]==0)
104 {
105 for(k=1;i;i=tmp1[i],k++)
106 if(k&1)
107 ans+=i;
108 printf("Case %d: %lld\n",p++,ans);
109 break;
110 }
111 }
112 }
113 return 0;
114 }