#import <Foundation/Foundation.h>
#import <stdlib.h>
#import <stdarg.h>

typedef NSInteger Int;
typedef NSUInteger UINT;

/// Utility functions //////////////////////////////////////////////////

Int min(Int len, ...) { // 可変長のmin。第1引数は比較するものの数。
	Int _min, num;
	va_list ap;
	va_start(ap, len);

	_min = va_arg(ap, Int);
	for (Int i = 1; i < len; i++) {
		num = va_arg(ap, Int);
		if (num < _min)
			_min = num;
	}
	va_end(ap);
	return _min;
}

Int min2(Int a, Int b) {
	return min(2, a, b);	
}

Int max(Int len, ...) { // 可変長のmax。第1引数は比較するものの数。
	Int _max, num;
	va_list ap;
	va_start(ap, len);

	_max = va_arg(ap, Int);
	for (Int i = 1; i < len; i++) {
		num = va_arg(ap, Int);
		if (num > _max)
			_max = num;
	}
	va_end(ap);
	return _max;
}

Int max2(Int a, Int b) {
	return max(2, a, b);	
}

Int ipow(Int x, Int n) {
	int result = 1;
	while (n > 0) {
		if ((n % 2) == 0) {
			x *= x;
			n >>= 1;
		} else {
			result *= x;
			n--;
		}
	}
	return result;
}

/// math.h の代替  //////////////////////////////////////////////////
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

inline double fabs(double x) {
	return (x>=0) ? x : -x;
}

inline double sqrt(double x) {
	double         xHalf = 0.5 * x;
	long long int  tmp   = 0x5FE6EB50C7B537AAl - ( *(long long int*)&x >> 1);//initial guess
	double         xRes  = * (double*)&tmp;

	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	return xRes * x;
}

inline double sin(double x){
	int i = 1;
	double cur = x;
	double acc = 1;
	double fact= 1;
	double pow = x;
	while (fabs(acc) > .00000001 && i < 100){
		fact *= ((2*i)*(2*i+1));
		pow *= -1 * x*x; 
		acc =  pow / fact;
		cur += acc;
		i++;
	}
	return cur;
}

inline double cos(double x) {
	return sin(M_PI/2 - x);
}

/// @interface //////////////////////////////////////////////////
@interface NSArray (List)
-(Int)integerValueAtIndex:(UINT)index;
-(Int)firstInteger;
-(Int)secondInteger;
-(Int)thirdInteger;
-(Int)fourthInteger;
-(NSMutableArray*)sortedArrayWithAscending:(BOOL)ascending; // NSNumber* を要素に持つ配列をソート
-(NSMutableArray*)unique;
@end

@interface NSMutableArray (List)
+(NSMutableArray*)arrayWithNumbers:(Int)count, ...; // 要素数, 数1, 数2, ... から NSMutableArray<NSNumber*> オブジェクトを生成
-(NSMutableArray*)unshift:(id)element;
-(id)shift;
-(NSMutableArray*)push:(id)element;
-(id)pop;
@end

@interface NSString (Split)
-(NSMutableArray*)componentsSeparatedByCharacter:(unichar)separator;
-(NSMutableArray*)numbersSeparatedByString:(NSString*)separator;
-(NSMutableArray*)numbersSeparatedByCharacter:(unichar)separator;
-(NSMutableArray*)splitCharacterEvery:(UINT)number;
-(NSMutableArray*)characterStrings; // 1文字ずつの NSString* の配列を返す
-(NSMutableArray*)characters; // 1文字ずつの unichar をラップした NSNumber の配列を返す
-(unichar)firstCharacter;
-(unichar)lastCharacter;
-(NSString*)leadingSubstringWithLength:(UINT)length;
-(NSString*)trailingSubstringWithLength:(UINT)length;
@end

@interface NSMutableString (List)
-(NSMutableString*)unshift:(NSString*)string;
-(NSString*)shiftWithLength:(UINT)length;
-(NSMutableString*)push:(NSString*)string;
-(NSString*)popWithLength:(UINT)length;
@end

@interface CharGrid : NSObject
{
	Int _width;
	Int _height;
	NSMutableArray *values; // 「1文字ずつの unichar をラップした NSNumber の配列」の配列（2次元配列）
}
-(instancetype)initWithTwoDimensionalArray:(NSArray*)array; // 「1文字ずつの unichar をラップした NSNumber の配列」の配列（2次元配列）で初期化
-(instancetype)initWithLines:(NSArray*)lines; // 「1行分を表す文字列の配列（1次元配列）で初期化
-(Int)width;
-(Int)height;
-(char)characterAtX:(Int)x Y:(Int)y;
-(void)setCharacter:(char)value atX:(UINT)x Y:(UINT)y;
-(void)setInteger:(Int)value atX:(UINT)x Y:(UINT)y; // 数値を文字化してノードにセットする
-(NSString*)description;
@end

@interface IntegerGrid : NSObject
{
	Int _width;
	Int _height;
	NSMutableArray *values; // 「1ノードの数値の Int をラップした NSNumber の配列」の配列（2次元配列）
}
-(instancetype)initWithTwoDimensionalArray:(NSArray*)array; // 「1ノードの数値の Int をラップした NSNumber の配列」の配列（2次元配列）で初期化
-(instancetype)initWithLines:(NSArray*)lines; // 「1行分を表す文字列（スペース区切り数値の列）の配列（1次元配列）で初期化
-(Int)width;
-(Int)height;
-(Int)valueAtX:(Int)x Y:(Int)y;
-(void)setValue:(Int)value atX:(UINT)x Y:(UINT)y;
-(NSString*)description;
@end

@interface ClosedInterval : NSObject
{
	Int _start;
	Int _end;
}
-(instancetype)initWithStart:(Int)start end:(Int)end;
-(Int)start;
-(Int)end;
-(Int)length;
-(ClosedInterval*)intersectionWith:(ClosedInterval*)interval;
-(BOOL)containsInterval:(ClosedInterval*)interval;
@end

@interface Vector2D : NSObject
{
	double _x;
	double _y;
}
-(instancetype)initWithX:(double)x Y:(double)y;
-(instancetype)copy;
-(double)x;
-(double)y;
-(void)setX:(double)x;
-(void)setY:(double)y;
-(Vector2D*)add:(Vector2D*)vector;
-(Vector2D*)subtract:(Vector2D*)vector;
-(Vector2D*)multiply:(double)scale;
-(double)innerProduct:(Vector2D*)vector;
-(Vector2D*)rotateWithDegree:(double)degree;
-(double)abs;
-(double)distanceFrom:(Vector2D*)point;
-(NSString*)description;
@end

@interface StdIO : NSObject
+(NSMutableString*)stringFromStdIn;
+(NSMutableArray*)stringsFromStdInSeparatedByString:(NSString*)separator;
+(NSMutableArray*)stringsFromStdInSeparatedByCharacter:(unichar)separator;
+(NSMutableArray*)numbersFromStdInSeparatedByString:(NSString*)separator;
+(NSMutableArray*)numbersFromStdInSeparatedByCharacter:(unichar)separator;
+(void)put:(char*)format, ...;
+(void)puts:(NSString*)format, ...;
@end


/// @implementation //////////////////////////////////////////////////

@implementation NSArray (List)
-(Int)integerValueAtIndex:(UINT)index
{
	return [[self objectAtIndex:index] integerValue];
}

-(Int)firstInteger
{
	return [self integerValueAtIndex: 0];
}

-(Int)secondInteger
{
	return [self integerValueAtIndex: 1];
}

-(Int)thirdInteger
{
	return [self integerValueAtIndex: 2];
}

-(Int)fourthInteger
{
	return [self integerValueAtIndex: 3];
}

-(NSMutableArray*)sortedArrayWithAscending:(BOOL)ascending
{
	NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey: @"self" ascending: ascending];
	NSArray *descriptors = [NSArray arrayWithObject: descriptor];
	NSArray *sortedArray = [self sortedArrayUsingDescriptors: descriptors];
	return [NSMutableArray arrayWithArray: sortedArray];
}

-(NSMutableArray*)unique
{
	NSMutableArray *result = [NSMutableArray array];
	NSEnumerator *enumerator = [self objectEnumerator];
	id object;
	while ((object = [enumerator nextObject])) {
		if (![result containsObject: object]) {
			[result push: object];
		}
	}
	return result;
}
@end

@implementation NSMutableArray (List)
+(NSMutableArray*)arrayWithNumbers:(Int)count, ...
{
	va_list numbers;
	va_start(numbers, count);
	NSMutableArray *array = [NSMutableArray array];
	
	for (Int i = 0; i < count; i++) {
		[array push: [NSNumber numberWithInt:va_arg(numbers, Int)]];
	}
	
	va_end(numbers);	
	return array;
}

-(NSMutableArray*)unshift:(id)element
{
	[self insertObject: element atIndex: 0];
	return self;
}

-(id)shift
{
	if ([self count] > 0) {
		id first = [self objectAtIndex: 0];
		[self removeObjectAtIndex: 0];
		return first;
	} else {
		return nil;
	}
}

-(NSMutableArray*)push:(id)element
{
	[self addObject: element];
	return self;
}

-(id)pop
{
	UINT count = [self count];
	if (count > 0) {
		id last = [self objectAtIndex: count - 1];
		[self removeLastObject];
		return last;
	} else {
		return nil;
	}
}
@end


@implementation NSString (Split)
-(NSMutableArray*)componentsSeparatedByCharacter:(unichar)separator
{
	return [NSMutableArray arrayWithArray: [self componentsSeparatedByString:[NSString stringWithFormat:@"%c", separator]]];
}

-(NSMutableArray*)numbersSeparatedByString:(NSString*)separator
{
	NSArray *strings = [self componentsSeparatedByString: separator];
	NSMutableArray *numbers = [NSMutableArray array];
	for (NSString *string in strings) {
		if (![string isEqualToString:@""]) {
			[numbers addObject: [NSNumber numberWithInteger: [string integerValue]]];
		}
	}
	return numbers;
}

-(NSMutableArray*)numbersSeparatedByCharacter:(unichar)separator
{
	return [self numbersSeparatedByString:[NSString stringWithFormat:@"%c", separator]];
}

-(NSMutableArray*)splitCharacterEvery:(UINT)number
{
	if ([self length] <= number) {
		return [NSMutableArray arrayWithObject: self];
	}

	NSMutableArray  *mArray = [NSMutableArray array];
	NSMutableString *mStr   = [NSMutableString stringWithString: self];

	NSRange range = NSMakeRange(0, number);

	while ([mStr length] > 0) {
		if ([mStr length] < number) {
			[mArray addObject: [NSString stringWithString: mStr]];
			[mStr deleteCharactersInRange:NSMakeRange(0, [mStr length])];
		} else {
			[mArray addObject: [mStr substringWithRange: range]];
			[mStr deleteCharactersInRange: range];
		}
	}

	return mArray;
}

-(NSMutableArray*)characterStrings {
	return [self splitCharacterEvery: 1];
}

-(NSMutableArray*)characters {
	NSMutableArray *result = [NSMutableArray array];
	for (UINT i=0; i<[self length]; i++) {
		[result push: @([self characterAtIndex: i])];
	}
	return result;
}

-(unichar)firstCharacter
{
	return [self characterAtIndex: 0];
}

-(unichar)lastCharacter
{
	return [self characterAtIndex: [self length] - 1];
}

-(NSString*)leadingSubstringWithLength:(UINT)length
{
	if (length <= [self length]) {
		return [self substringWithRange: NSMakeRange(0, length)];
	} else {
		return nil;
	}
	
}

-(NSString*)trailingSubstringWithLength:(UINT)length
{
	UINT mylength = [self length];
	if (length <= mylength) {
		return [self substringWithRange: NSMakeRange(mylength - length, length)];
	} else {
		return nil;
	}
}

@end

@implementation NSMutableString (List)
-(NSMutableString*)unshift:(NSString*)string
{
	[self insertString: string atIndex: 0];
	return self;
}

-(NSString*)shiftWithLength:(UINT)length
{
	if ([self length] >= length) {
		NSRange range = NSMakeRange(0, length);
		NSString *first = [self substringWithRange: range];
		[self deleteCharactersInRange: range];
		return first;
	} else {
		return nil;
	}
}

-(NSString*)push:(NSString*)string
{
	[self appendString: string];
	return self;
}

-(NSString*)popWithLength:(UINT)length
{
	UINT mylength = [self length];
	if (length <= mylength) {
		NSRange range = NSMakeRange(mylength - length, length);
		NSString *last = [self substringWithRange: range];
		[self deleteCharactersInRange: range];
		return last;
	} else {
		return nil;
	}
}
@end

@implementation CharGrid
-(Int)width
{
	return _width;
}

-(Int)height
{
	return _height;
}

-(instancetype)initWithTwoDimensionalArray:(NSArray*)array
{
	if (self = [super init]) {
		_width = [(NSArray*)[array objectAtIndex: 0] count];
		_height = [array count];
		values = [NSMutableArray arrayWithArray: array];
	}
	return self;
}

-(instancetype)initWithLines:(NSArray*)lines
{
	if (self = [super init]) {
		values = [NSMutableArray array];
		for (NSString *line in lines) {
			[values push: [line characters]];
		}
		_width = [(NSArray*)[values objectAtIndex: 0] count];
		_height = [values count];
	}
	return self;
}

-(char)characterAtX:(Int)x Y:(Int)y
{
	if (x>=0 && y>=0 && x<_width && y<_height) {
		return [(NSNumber*)[(NSMutableArray*)[values objectAtIndex: y] objectAtIndex: x] charValue];
	} else {
		return 0;
	}
}

-(void)setCharacter:(char)value atX:(UINT)x Y:(UINT)y
{
	[(NSMutableArray*)[values objectAtIndex: y] replaceObjectAtIndex: x withObject: @(value)];
}

-(void)setInteger:(Int)value atX:(UINT)x Y:(UINT)y
{
	NSString *string = [NSString stringWithFormat:@"%ld", value];
	[(NSMutableArray*)[values objectAtIndex: y] replaceObjectAtIndex: x withObject: @([string characterAtIndex: 0])];
}

-(NSString*)description
{
	NSMutableString *result = [NSMutableString string];
	for (UINT y=0; y<_height; y++) {
		for (UINT x=0; x<_width; x++) {
			[result appendFormat: @"%c", [self characterAtX: x Y: y]];
		}
		[result push:@"\n"];
	}
	return result;
}
@end

@implementation IntegerGrid
-(instancetype)initWithTwoDimensionalArray:(NSArray*)array
{
	if (self = [super init]) {
		_width = [(NSArray*)[array objectAtIndex: 0] count];
		_height = [array count];
		values = [NSMutableArray arrayWithArray: array];
	}
	return self;
}

-(instancetype)initWithLines:(NSArray*)lines
{
	if (self = [super init]) {
		values = [NSMutableArray array];
		for (NSString *line in lines) {
			[values push: [line numbersSeparatedByCharacter:' ']];
		}
		_width = [(NSArray*)[values objectAtIndex: 0] count];
		_height = [values count];
	}
	return self;
}

-(Int)width
{
	return _width;
}

-(Int)height
{
	return _height;
}

-(Int)valueAtX:(Int)x Y:(Int)y
{
	if (x>=0 && y>=0 && x<_width && y<_height) {
		return [(NSNumber*)[(NSMutableArray*)[values objectAtIndex: y] objectAtIndex: x] integerValue];
	} else {
		return 0;
	}
}

-(void)setValue:(Int)value atX:(UINT)x Y:(UINT)y
{
	[(NSMutableArray*)[values objectAtIndex: y] replaceObjectAtIndex: x withObject: @(value)];
}

-(NSString*)description
{
	NSMutableString *result = [NSMutableString string];
	for (UINT y=0; y<_height; y++) {
		NSMutableArray *integers = [NSMutableArray array];
		for (UINT x=0; x<_width; x++) {
			[integers push: @([self valueAtX: x Y: y])];
		}
		[result push: [integers componentsJoinedByString:@" "]];
		[result push: @"\n"];
	}
	return result;
}
@end


@implementation ClosedInterval
-(instancetype)initWithStart:(Int)start end:(Int)end
{
	if (self = [super init]) {
		_start = start;
		_end = end;
		if (_start > _end) {
			return nil;
		}
	}
	return self;
}

-(Int)start
{
	return _start;
}

-(Int)end
{
	return _end;
}

-(Int)length
{
	return _end - _start;
}

-(ClosedInterval*)intersectionWith:(ClosedInterval*)interval
{
	Int a = max2(_start, [interval start]);
	Int b = min2(_end, [interval end]);
	return [[ClosedInterval alloc] initWithStart: a end: b];
}

-(BOOL)containsInterval:(ClosedInterval*)interval 
{
	return (_start <= [interval start]) && ([interval end] <= _end);
}

-(NSString*)description
{
	return [NSString stringWithFormat:@"[%ld, %ld]", _start, _end];
}
@end

@implementation Vector2D
-(instancetype)initWithX:(double)x Y:(double)y
{
	if (self = [super init]) {
		_x = x;
		_y = y;
	}
	return self;
}

-(instancetype)copy
{
	return [[Vector2D alloc] initWithX: _x Y: _y];
}

-(double)x
{
	return _x;
}

-(double)y
{
	return _y;
}

-(void)setX:(double)x
{
	_x = x;
}

-(void)setY:(double)y
{
	_y = y;
}

-(Vector2D*)add:(Vector2D*)vector
{
	double x = _x + [vector x];
	double y = _y + [vector y];
	return [[Vector2D alloc] initWithX: x Y: y];
}

-(Vector2D*)subtract:(Vector2D*)vector
{
	double x = _x - [vector x];
	double y = _y - [vector y];
	return [[Vector2D alloc] initWithX: x Y: y];
}


-(Vector2D*)multiply:(double)scale
{
	double x = _x * scale;
	double y = _y * scale;
	return [[Vector2D alloc] initWithX: x Y: y];
}

-(double)innerProduct:(Vector2D*)vector
{
	return _x * [vector x] + _y * [vector y];
}

-(Vector2D*)rotateWithDegree:(double)degree
{
	double c = cos(degree * M_PI/180);
	double s = sin(degree * M_PI/180);
	return [[Vector2D alloc] initWithX: (c * _x - s * _y) Y: (s * _x + c * _y)];
}

-(double)abs
{
	return sqrt(_x * _x + _y * _y);
}

-(double)distanceFrom:(Vector2D*)point
{
	return [[self subtract: point] abs];
}

-(NSString*)description
{
	return [NSString stringWithFormat:@"(%f, %f)", _x, _y];
}
@end

@implementation StdIO
+(NSMutableString*)stringFromStdIn
{
	NSFileHandle *handle = [NSFileHandle fileHandleWithStandardInput];
	NSMutableString *result = [NSMutableString string];
	NSData *data = [handle availableData];
	while ([data length] > 0) {
		[result appendString:[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]];
		data = [handle availableData];
	}

	if ([result lastCharacter] == '\n') {
		[result popWithLength: 1];
	}
	return result;
}

+(NSMutableArray*)stringsFromStdInSeparatedByString:(NSString*)separator
{
	return [NSMutableArray arrayWithArray:[[StdIO stringFromStdIn] componentsSeparatedByString: separator]];
}

+(NSMutableArray*)stringsFromStdInSeparatedByCharacter:(unichar)separator
{
	return [NSMutableArray arrayWithArray:[[StdIO stringFromStdIn] componentsSeparatedByCharacter: separator]];
}

+(NSMutableArray*)numbersFromStdInSeparatedByString:(NSString*)separator
{
	return [[StdIO stringFromStdIn] numbersSeparatedByString: separator];
}

+(NSMutableArray*)numbersFromStdInSeparatedByCharacter:(unichar)separator
{
	return [[StdIO stringFromStdIn] numbersSeparatedByCharacter: separator];
}

+(void)putsToStdOut:(NSString*)string
{
	NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardOutput];
	[fileHandle writeData:[string dataUsingEncoding: NSUTF8StringEncoding]];
	[fileHandle writeData:[@"\n" dataUsingEncoding: NSUTF8StringEncoding]];
}

+(void)put:(char*)format, ...
{
	va_list arguments;
	va_start(arguments, format);
	NSString *string = [[NSString alloc] initWithFormat: [NSString stringWithUTF8String: format] arguments: arguments];
	[StdIO putsToStdOut: string];
	va_end(arguments);	
}

+(void)puts:(NSString*)format, ...
{
	va_list arguments;
	va_start(arguments, format);
	NSString *string = [[NSString alloc] initWithFormat: format arguments: arguments];
	[StdIO putsToStdOut: string];
	va_end(arguments);	
}
@end

/////////////////////////////////////////////////////////////////////////////////

int main(int argc, const char * argv[]) {
	@autoreleasepool {
			NSMutableArray *numbers = [StdIO numbersFromStdInSeparatedByCharacter:'\n'];
			Int N = [numbers firstInteger];
			[numbers shift];
			Int current = 0;
			for (Int i=0; i<N; i++) {
				current = [numbers integerValueAtIndex: current]-1;
				if (current == 1) {
					[StdIO put:"%ld", i+1];
					return 0;
				}
			}
			[StdIO put:"-1"];
	}
	return 0;
}

Measured memory_bytes: 19004