#include #include #include #include using namespace std; struct Point{ Point(int a, int b) : x(a), y (b) {} friend bool operator<(const Point & a, const Point & b) { return ((a.x == b.x ) && ( a.y < b.y )) || a.x < b.x; } public: int x, y; }; void move (Point & current, int direction) { switch (direction) { case 0: current.y++; break; case 1: current.x++; break; case 2: current.y--; break; case 3: current.x--; break; } } int main() { int numInstr; while ( scanf("%d", &numInstr) == 1 ) { set visited; Point current(0,0); visited.insert(current); bool isEnd = false; int direction = 3; // 0 for sever, 1 for vychod, 2 for jih, 3 for zapad int steps = 0; string str; for ( int i = 0; i < numInstr; i ++) { scanf("%d", &steps); direction = (direction + 1)%4; for ( int j = 0; j < steps; j++ ) { move(current, direction); if ( visited.find(current) != visited.end() ) { cout << i << endl; isEnd = true; visited.clear(); getline(cin, str); break; } visited.insert(current); } if ( isEnd ) break; } if ( !isEnd ) cout << "OK" << endl; } return 0; }