# your code goes here#include "bits/stdc++.h"
using namespace std;
int n;
int a[100005];
pair<int,int> p[100005];
int maxi[100005];
int main()
{
scanf("%d",&n);
for(int i = 1 ; i <= n ; i++)
{
scanf("%d",&a[i]);
p[i]={a[i],i};
}
sort(p+1,p+n+1);
maxi[n] = p[n].second;
for(int i = n - 1 ; i >= 1 ; i--)
{
maxi[i] = max(maxi[i+1], p[i].second);
}
int ans = -1;
for(int i = 1 ; i < n ; i++)
{
if(maxi[i+1]>p[i].second)
ans=max(ans,maxi[i+1]-p[i].second);
}
cout<<ans;
return 0;
}
IyB5b3VyIGNvZGUgZ29lcyBoZXJlI2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbjsKaW50IGFbMTAwMDA1XTsKcGFpcjxpbnQsaW50PiBwWzEwMDAwNV07CmludCBtYXhpWzEwMDAwNV07CgppbnQgbWFpbigpCnsKICAgIHNjYW5mKCIlZCIsJm4pOwogICAgZm9yKGludCAgaSA9IDEgOyBpIDw9IG4gOyBpKyspCiAgICB7CiAgICAgICAgc2NhbmYoIiVkIiwmYVtpXSk7CiAgICAgICAgcFtpXT17YVtpXSxpfTsKICAgIH0KICAgIHNvcnQocCsxLHArbisxKTsKICAgIG1heGlbbl0gPSBwW25dLnNlY29uZDsKICAgIGZvcihpbnQgaSA9IG4gLSAxIDsgaSA+PSAxIDsgaS0tKQogICAgewogICAgICAgIG1heGlbaV0gPSBtYXgobWF4aVtpKzFdLCBwW2ldLnNlY29uZCk7CiAgICB9CiAgICBpbnQgYW5zID0gLTE7CiAgICBmb3IoaW50IGkgPSAxIDsgaSA8IG4gOyBpKyspCiAgICB7CiAgICAgICAgaWYobWF4aVtpKzFdPnBbaV0uc2Vjb25kKQogICAgICAgICAgICBhbnM9bWF4KGFucyxtYXhpW2krMV0tcFtpXS5zZWNvbmQpOwogICAgfQogICAgY291dDw8YW5zOwogICAgcmV0dXJuIDA7Cn0=
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/py_compile.py", line 117, in compile
raise py_exc
py_compile.PyCompileError: File "prog.py", line 2
using namespace std;
^
SyntaxError: invalid syntax