#include <iostream>
using namespace std;
#include<stdio.h>
#include<stdlib.h>
typedef struct ANTs
{
int address;
int speed;
int Isfull = 0;
int crash;
}ANTs, * ANTsList;
int main()
{
int n = 4;
int notfull;
int a;
ANTsList ants;
scanf("%d", &n);
ants = (ANTsList)malloc(n * sizeof(ANTs));
for (int i = 0; i < n; i++)
{
ants[i].address = 10; ants[i].speed = 1; ants[i].Isfull = 0; ants[i].crash = 0;
scanf("%d %d", &ants[i].address, &ants[i].speed);
if (ants[i].speed == 0)
{
a = i;
}
}
//ants = (ANTsList)malloc(n * sizeof(ANTs));
//ants[0].address = 10; ants[0].speed = 1; ants[0].Isfull = 0; ants[0].crash = 0;
//ants[1].address = 90; ants[1].speed = 0; ants[1].Isfull = 0; ants[1].crash = 0;
//ants[2].address = 95; ants[2].speed = -1; ants[2].Isfull = 0; ants[2].crash = 0;
//ants[3].address = 98; ants[3].speed = -1; ants[3].Isfull = 0; ants[3].crash = 0;
//a = 1;
notfull = n;
int k = 0;
int step = 0;
bool needgo = true;
while (needgo)
{
int stick[101][10] = { 0 };
int stickNum[101] = { 0 };
if (step == 0)
{
for (int i = 0; i < n; i++) {
stick[ants[i].address][stickNum[ants[i].address]++] = i;
}
}
else
{
for (int i = 0; i < n; i++)
{
if (ants[i].Isfull == 0)
{
if (ants[i].crash == 0)
{
ants[i].address += ants[i].speed;
}
else
{
ants[i].speed = -ants[i].speed;
ants[i].crash = 0;
}
if (ants[i].address == 0 || ants[i].address == 100)
{
ants[i].Isfull = 1;
notfull--;
if (notfull == 1 && ants[a].speed == 0)
{
printf("Cannot fall!");
needgo = false;
break;
}
if (i == a)
{
printf("%d", step);
needgo = false;
break;
}
}
else
{
stick[ants[i].address][stickNum[ants[i].address]++] = i;
}
}
}
}
step++;
int total = 0;
int totalTemp = 0;
for (int i = 0; i < 101; i++)
{
if (stickNum[i] > 3)
{
cout << "有意外情况" << i << " " << step << " " << stickNum[i] << endl;
}
if (stickNum[i] == 3)
{
int totalSpeed3 = 0;
for (int j = 0; j < stickNum[i]; j++)
{
totalSpeed3 += ants[stick[i][j]].speed;
if (ants[stick[i][j]].speed == 1)
{
ants[stick[i][j]].speed = -1;
}
else
if (ants[stick[i][j]].speed == -1)
{
ants[stick[i][j]].speed = 1;
}
}
if (totalSpeed3 != 0)
{
cout << "有意外情况" << i << " " << step << " " << stickNum[i] << endl;
}
}
if (stickNum[i] == 2)
{
int speed = ants[stick[i][0]].speed;
ants[stick[i][0]].speed = ants[stick[i][1]].speed;
ants[stick[i][1]].speed = speed;
}
if (stickNum[i] >= 2 && step == 1) {
cout << " 初始有相同位置的蚂蚁" << i << endl;
}
}
for (int i = 0; i < 101; i++)
{
if (stickNum[i] != 0)
{
total++;
}
else
{
total = 0;
}
if (total >= 2)
{
/*totalTemp = ants[stick[i - 2][0]].speed;
ants[stick[i - 2][0]].speed = ants[stick[i - 1][0]].speed;
ants[stick[i - 1][0]].speed = totalTemp;
totalTemp = 0;*/
for (int m1 = 0; m1 < stickNum[i]; m1++)
{
for (int m2 = 0; m2 < stickNum[i - 1]; m2++)
{
ANTs* ant3 = &ants[stick[i][m1]];
ANTs* ant0 = &ants[stick[i - 1][m2]];
if (ant3->speed == -1 && ant0->speed == 1)
{
ant3->crash = 1;
ant0->crash = 1;
}
}
}
}
//if (total == 3)
//{
// ANTs ant3 = ants[stick[i - 2][0]];
// ANTs ant0 = ants[stick[i][0]];
// totalTemp = ant3.speed;
// ant3.speed = ant0.speed;
// ant0.speed = totalTemp;
// totalTemp = 0;
//}
}
}
return 0;
}