unittest
{
	assert( [ "4", "1", "3", "2", "4" ].parse.expand.solve == 2 );
	assert( [ "6", "3", "2", "5", "1", "4", "6" ].parse.expand.solve == 4 );
	assert( [ "8", "6", "3", "1", "2", "7", "4", "8", "5" ].parse.expand.solve == 5 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	auto ps = new long[ n ];
	foreach( ref p; ps )
	{
		input.popFront;
		p = input.front.to!( long );
	}
	return tuple( ps );
}

auto solve( long[] ps )
{
	long[ long ] as;
	foreach( p; ps )
	{
		if( p - 1 in as )
		{
			as[ p ] = as[ p - 1 ] + 1;
			as.remove( p - 1 );
		}
		else
		{
			as[ p ] = 1;
		}
	}
	return ps.length - as.values.reduce!max;
}
