using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
KidCircle kc = new KidCircle(500);
int countNum = 0;
Kid k = kc.first;
while (kc.count > 1)
{
countNum++;
if (countNum == 3) {
countNum = 0;
kc.Delete(k);
}
k = k.right;
}
Console.WriteLine(kc.first.id);
Console.ReadKey();
}
}
class KidCircle
{
public int count;
public Kid first, last;
public KidCircle(int n)
{
for (int i = 0; i < n; i++)
Add();
}
public void Delete(Kid k)
{
k.left.right = k.right;
k.right.left = k.left;
if (k ==first)
first = k.right;
if (k== last)
last = k.left;
count--;
}
void Add()
{
Kid k = new Kid(); k.id = count;
if (count <= 0)
{
first = last = k;
k.left = k; k.right = k;
}
else
{
k.left = last;
k.right = first;
last.right = k;
first.left = k;
last = k;
}
count++;
}
}
class Kid
{
internal int id;
internal Kid left, right;
}
}