#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=n-1,k=0;

    i=0;
    int l=1;
    for(;i<n;i++)
    {
        for(;j>=0;j--)
        {
            if(x<a[j]||x<a[i])
            {
                if(a[j]>x&&x>a[i])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
                if(a[i]>x&&x>a[j])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[j]>a[i])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[i]>a[j])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
            }
            l=1;
            break;
        }
        if(l==1)
            break;
    }

    printf("%d\n",k);
    s[k]='\0';
    printf("%s",s);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=0;

    for(i=0;i<n;i++)
    {
        if(a[i]>a[n-1-i]&&a[n-i-1]>x)
        {
            x=a[n-i-1];
            s[i]='R';
            j++;
            continue;
        }
        if(a[i]<a[n-1-i]&&x<a[i])
        {
            x=a[i];
            s[i]='L';
            j++;
            continue;
        }
        break;
    }

    printf("%d",j);
    s[k+1]='\0';

    printf("%s",s);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=n-1,k=0;

    i=0;
    int l=0;
    for(;i<n;i++)
    {
        for(;j>=0;j--)
        {
            if(a[i]>a[j]&&x<a[j])
            {
                l=0;
                x=a[j];
                s[k]='R';
                k++;
                continue;
            }
            if(a[j]>a[i]&&x<a[i])
            {
                l=0;
                x=a[i];
                s[k]='L';
                k++;
                break;
            }
            l=1;
            break;
        }
        if(l==1)
            break;
    }

    printf("%d\n",k);
    s[k]='\0';
    printf("%s",s);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=n-1,k=0;

    i=0;
    int l=1;
    for(;i<n;i++)
    {
        for(;j>=0;j--)
        {
            if(x<=a[j]||x<=a[i])
            {
                if(a[j]>=x&&x>=a[i])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
                if(a[i]>=x&&x>=a[j])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[j]>=a[i])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[i]>=a[j])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
            }
            l=1;
            break;
        }
        if(l==1)
            break;
    }

    printf("%d\n",k);
    s[k]='\0';
    printf("%s",s);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=n-1,k=0;

    i=0;
    int l=1;
    for(;i<n;i++)
    {
        for(;j>=0;j--)
        {
            if(k==n)
            {
                l=1;
                break;
            }
            if(x<=a[j]||x<=a[i])
            {
                if(a[j]>=x&&x>=a[i])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
                if(a[i]>=x&&x>=a[j])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[j]>=a[i])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[i]>=a[j])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
            }
            l=1;
            break;
        }
        if(l==1)
            break;
    }

    printf("%d\n",k);
    s[k]='\0';
    printf("%s",s);
    return 0;
}


#include <stdio.h>

int main()
{
	int arr[100000],n,start,end,temp=0,count=-1,i,j;
	char arr1[100000];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{

	scanf("%d",&arr[i]);
//	printf("%d",arr[i]);
      }
	start=0;
	end=n-1;

	 /*
	  if(arr[start]<arr[end])
	  {
	  temp=arr[start];
	  start++;
	  count++;
	  arr1[count]=76;
	  }
	  else
	  {

	  temp=arr[end];
	  count++;
	  end--;
	  arr1[count]=82;

	  }
	  */
	  //&&(arr[end]-temp>0)&&(arr[start]-temp>0)
	while(start!=end)
	{

	    if(arr[start]-temp>arr[end]-temp)

	    {
	    	if(arr[end]>temp)
	    	{

	        temp=arr[end];
	        count+=1;
	        end--;
	        arr1[count]=82;
	       //printf("n\n");
	        }
	        else if(arr[start]>temp)
	        {
	        temp=arr[start];
	        count++;
	        start++;
	        arr1[count]=76;
	        }

	       else
		   break;
	    }
	    //&&(arr[start]-temp>0)&&(arr[end]-temp>0)
	    else if(arr[start]-temp<arr[end]-temp)
	    {
	       if(arr[start]>temp)
	       {

		    temp=arr[start];
	        count+=1;
	        start++;
	        arr1[count]=76;
	         //printf("bgn\n");
	    }

	    else if(arr[end]>temp)
	        {

	        temp=arr[end];
	        count++;
	        end--;
	        arr1[count]=82;
	        }
	      else
		  break;
		}
		//((arr[start]<temp)&&(arr[end]<temp))
	   else
	    {
	    	//printf("ab\n");
		break;
	    }
    }

   if(start==end&&temp<arr[start])
   {
 arr1[++count]=76;

   }

	for( j=0;j<=count;j++)
	printf("%c",arr1[j]);

	return 0;
}


#include <stdio.h>

int main()
{
	int arr[900000],n,start,end,temp=0,count=-1,i,j;
	char arr1[900000];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{

	scanf("%d",&arr[i]);
//	printf("%d",arr[i]);
      }
	start=0;
	end=n-1;

	 /*
	  if(arr[start]<arr[end])
	  {
	  temp=arr[start];
	  start++;
	  count++;
	  arr1[count]=76;
	  }
	  else
	  {

	  temp=arr[end];
	  count++;
	  end--;
	  arr1[count]=82;

	  }
	  */
	  //&&(arr[end]-temp>0)&&(arr[start]-temp>0)
	while(start!=end)
	{

	    if(arr[start]-temp>arr[end]-temp)

	    {
	    	if(arr[end]>temp)
	    	{

	        temp=arr[end];
	        count+=1;
	        end--;
	        arr1[count]=82;
	       //printf("n\n");
	        }
	        else if(arr[start]>temp)
	        {
	        temp=arr[start];
	        count++;
	        start++;
	        arr1[count]=76;
	        }

	       else
		   break;
	    }
	    //&&(arr[start]-temp>0)&&(arr[end]-temp>0)
	    else if(arr[start]-temp<arr[end]-temp)
	    {
	       if(arr[start]>temp)
	       {

		    temp=arr[start];
	        count+=1;
	        start++;
	        arr1[count]=76;
	         //printf("bgn\n");
	    }

	    else if(arr[end]>temp)
	        {

	        temp=arr[end];
	        count++;
	        end--;
	        arr1[count]=82;
	        }
	      else
		  break;
		}
		//((arr[start]<temp)&&(arr[end]<temp))
	   else
	    {
	    	//printf("ab\n");
		break;
	    }
    }

   if(start==end&&temp<arr[start])
   {
 arr1[++count]=76;

   }
   printf("%d\n",count+1);

	for( j=0;j<=count;j++)
	printf("%c",arr1[j]);

	return 0;
}


#include <stdio.h>

int main()
{
	int arr[100000],n,start,end,temp=0,count=-1,i,j;
	char arr1[100000];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{

	scanf("%d",&arr[i]);
//	printf("%d",arr[i]);
      }
	start=0;
	end=n-1;

	 /*
	  if(arr[start]<arr[end])
	  {
	  temp=arr[start];
	  start++;
	  count++;
	  arr1[count]=76;
	  }
	  else
	  {

	  temp=arr[end];
	  count++;
	  end--;
	  arr1[count]=82;

	  }
	  */
	  //&&(arr[end]-temp>0)&&(arr[start]-temp>0)
	while(start!=end)
	{

	    if(arr[start]-temp>arr[end]-temp)

	    {
	    	if(arr[end]>temp)
	    	{

	        temp=arr[end];
	        count+=1;
	        end--;
	        arr1[count]=82;
	       //printf("n\n");
	        }
	        else if(arr[start]>temp)
	        {
	        temp=arr[start];
	        count++;
	        start++;
	        arr1[count]=76;
	        }

	       else
		   break;
	    }
	    //&&(arr[start]-temp>0)&&(arr[end]-temp>0)
	    else if(arr[start]-temp<arr[end]-temp)
	    {
	       if(arr[start]>temp)
	       {

		    temp=arr[start];
	        count+=1;
	        start++;
	        arr1[count]=76;
	         //printf("bgn\n");
	    }

	    else if(arr[end]>temp)
	        {

	        temp=arr[end];
	        count++;
	        end--;
	        arr1[count]=82;
	        }
	      else
		  break;
		}
		//((arr[start]<temp)&&(arr[end]<temp))
	   else
	    {
	    	//printf("ab\n");
		break;
	    }
    }

   if(start==end&&temp<arr[start])
   {
 arr1[++count]=76;

   }
   printf("%d\n",count+1);

	for( j=0;j<=count;j++)
	printf("%c",arr1[j]);

	return 0;
}


#include <stdio.h>

int main(void)
{
	int N, i, j;
	int p = 0;
	int before = 0, count = 0;
	int number;
	int A[200010];
	char Print[200010];

	scanf("%d", &N);
	for(i = 0; i < N; i++)
	{
		scanf(" %d", &number);
		A[i] = number;
	}

	i = 0; j = N - 1;

	while(1)
	{
		if(A[i] < A[j])
		{
			if(A[i] > before)
			{
				before = A[i];
				i++;
				count++;
				Print[p++] = 'L';
			}
			else if(A[j] > before)
			{
				before = A[j];
				j--;
				count++;
				Print[p++] = 'R';
			}
			else
				break;
		}
		else if(A[i] > A[j])
		{
			if(A[j] > before)
			{
				before = A[j];
				j--;
				count++;
				Print[p++] = 'R';
			}
			else if(A[i] > before)
			{
				before = A[i];
				i++;
				count++;
				Print[p++] = 'L';
			}
			else
				break;
		}
		else if(i == j)
		{
			if(A[i] > before)
			{
				Print[p] = 'L';
				count++;
				break;
			}
			else
				break;
		}
	}
	printf("%d\n", count);
	for(i = 0; i < count; i++)
		printf("%c", Print[i]);

	return 0;
}


#include <stdio.h>
#define MAX 200001

int main() {
    int n, dem = 0;
    int i, j;
    scanf("%d", &n);
    int x[n], y[n];
    char s[MAX];
    for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }
    if (n == 1) {
        printf("1\nL");
        return 0;
    }
    i = 0; j = n - 1;
    while (i != j) {
        if (x[i] <= x[j]) {
            if(dem > 0) {
                if (x[j] <= y[dem-1]) break;
                else if (x[i] > y[dem - 1]) {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
                else {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
            }
            else {
                s[dem] = 'L';
                y[dem] = x[i];
                dem++;
                i++;
            }
        }
        else {
            if(dem > 0) {
                if (x[i] <= y[dem-1]) break;
                else if (x[j] > y[dem - 1]) {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
                else {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
            }
            else {
                s[dem] = 'R';
                y[dem] = x[j];
                dem++;
                j--;
            }
        }
    }
    if (i == j) {
        if (x[i] > y[dem-1]) {
            s[dem] = 'R';
            y[dem] = x[i];
            dem++;
        }
    }
    s[dem] = '\0';
    printf("%d\n", dem);
    printf("%s", s);
    return 0;
}


#include <stdio.h>
#define MAX 200001

int main() {
    int n, dem = 0;
    int i, j;
    scanf("%d", &n);
    int x[n], y[n];
    char s[MAX];
    for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }
    i = 0; j = n - 1;
    while (i != j) {
        if (x[i] <= x[j]) {
            if(dem > 0) {
                if (x[j] <= y[dem-1]) break;
                else if (x[i] > y[dem - 1]) {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
                else {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
            }
            else {
                s[dem] = 'L';
                y[dem] = x[i];
                dem++;
                i++;
            }
        }
        else {
            if(dem > 0) {
                if (x[i] <= y[dem-1]) break;
                else if (x[j] > y[dem - 1]) {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
                else {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
            }
            else {
                s[dem] = 'R';
                y[dem] = x[j];
                dem++;
                j--;
            }
        }
    }
    if (i == j) {
        s[dem] = 'R';
        dem++;
    }
    s[dem] = '\0';
    printf("%s", s);
    return 0;
}


#include <stdio.h>
#define MAX 200001

int main() {
    int n, dem = 0;
    int i, j;
    scanf("%d", &n);
    int x[n], y[n];
    char s[MAX];
    for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }
    i = 0; j = n - 1;
    while (i != j) {
        if (x[i] <= x[j]) {
            if(dem > 0) {
                if (x[j] <= y[dem-1]) break;
                else if (x[i] > y[dem - 1]) {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
                else {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
            }
            else {
                s[dem] = 'L';
                y[dem] = x[i];
                dem++;
                i++;
            }
        }
        else {
            if(dem > 0) {
                if (x[i] <= y[dem-1]) break;
                else if (x[j] > y[dem - 1]) {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
                else {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
            }
            else {
                s[dem] = 'R';
                y[dem] = x[j];
                dem++;
                j--;
            }
        }
    }
    if (i == j) {
        s[dem] = 'R';
        dem++;
    }
    s[dem] = '\0';
    printf("%d\n", dem);
    printf("%s", s);
    return 0;
}


#include <stdio.h>
#define MAX 200001

int main() {
    int n, dem = 0;
    int i, j;
    scanf("%d", &n);
    int x[n], y[n];
    char s[MAX];
    for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }
    i = 0; j = n - 1;
    while (i != j) {
        if (x[i] <= x[j]) {
            if(dem > 0) {
                if (x[j] <= y[dem-1]) break;
                else if (x[i] > y[dem - 1]) {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
                else {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
            }
            else {
                s[dem] = 'L';
                y[dem] = x[i];
                dem++;
                i++;
            }
        }
        else {
            if(dem > 0) {
                if (x[i] <= y[dem-1]) break;
                else if (x[j] > y[dem - 1]) {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
                else {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
            }
            else {
                s[dem] = 'R';
                y[dem] = x[j];
                dem++;
                j--;
            }
        }
    }
    s[dem] = '\0';
    printf("%d\n", dem);
    printf("%s", s);
    return 0;
}


#include <stdio.h>
#define MAX 200001

int main() {
    int n, dem = 0;
    int i, j;
    scanf("%d", &n);
    int x[n], y[n];
    char s[MAX];
    for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }
    i = 0; j = n - 1;
    while (i != j) {
        if (x[i] <= x[j]) {
            if(dem > 0) {
                if (x[j] <= y[dem-1]) break;
                else if (x[i] > y[dem - 1]) {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
                else {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
            }
            else {
                s[dem] = 'L';
                y[dem] = x[i];
                dem++;
                i++;
            }
        }
        else {
            if(dem > 0) {
                if (x[i] <= y[dem-1]) break;
                else if (x[j] > y[dem - 1]) {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
                else {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
            }
            else {
                s[dem] = 'R';
                y[dem] = x[j];
                dem++;
                j--;
            }
        }
    }
    s[dem] = '\0';
    printf("%s", s);
    return 0;
}


#include <stdio.h>
#define MAX 200001

int main() {
    int n, dem = 0;
    int i, j;
    scanf("%d", &n);
    int x[n], y[n];
    char s[MAX];
    for (i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }
    i = 0; j = n - 1;
    while (i != j) {
        if (x[i] <= x[j]) {
            if(dem > 0) {
                if (x[j] <= y[dem-1]) break;
                else if (x[i] > y[dem - 1]) {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
                else {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
            }
            else {
                s[dem] = 'L';
                y[dem] = x[i];
                dem++;
                i++;
            }
        }
        else {
            if(dem > 0) {
                if (x[i] <= y[dem-1]) break;
                else if (x[j] > y[dem - 1]) {
                    s[dem] = 'R';
                    y[dem] = x[j];
                    dem++;
                    j--;
                }
                else {
                    s[dem] = 'L';
                    y[dem] = x[i];
                    dem++;
                    i++;
                }
            }
            else {
                s[dem] = 'R';
                y[dem] = x[j];
                dem++;
                j--;
            }
        }
    }
    if (i == j) {
        if (x[i] > y[dem-1]) {
            s[dem] = 'R';
            y[dem] = x[i];
            dem++;
        }
    }
    s[dem] = '\0';
    printf("%d\n", dem);
    printf("%s", s);
    return 0;
}


#include <stdio.h>
#include <string.h>

int main() {

    int n, i, k = 0, pop;
    int a[200001];
    char frase[200002];

    scanf("%d", &n);

    for(i = 0; i < n; i++) {
        scanf(" %d", &a[i]);
    }

    int primeiro = 0;
    int ultimo = n-1;

    if(a[primeiro] <= a[ultimo]) {
        pop = a[primeiro];
        primeiro++;
        frase[k] = 'L';
    }
    else {
        pop = a[ultimo];
        ultimo--;
        frase[k] = 'R';
    }
    k++;
    //testar n = 1
    while(k < n) {

        if(a[primeiro] <= a[ultimo]) {

            if(a[primeiro] > pop) {
                pop = a[primeiro];
                primeiro++;
                frase[k] = 'L';
            }

            else if(a[ultimo] > pop){
                pop = a[ultimo];
                ultimo--;
                frase[k] = 'R';
            }

            else {
                break;
            }
        }

        else {

            if(a[ultimo] > pop) {
                pop = a[ultimo];
                ultimo--;
                frase[k] = 'R';
            }

            else if(a[primeiro] > pop) {
                pop = a[primeiro];
                primeiro++;
                frase[k] = 'L';
            }

            else {
                break;
            }
        }

        k++;
    }

    printf("%d\n", k);
    frase[k] = '\0';
    printf("%s", frase);

    return 0;
}


#include "stdio.h"
#define SIZE 200001

int n;
int arr[SIZE];
char choice[SIZE];
int calculate();

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}

	int result = calculate();
	printf("%d\n%s", result, choice);

	return 0;
}

int calculate() {
	int left = 0, right = n - 1;
	int answer = 0;
	int previous = 0;

	while (left <= right) {
		if (arr[left] > previous && (arr[left] < arr[right] || arr[right] < previous)) {
			choice[answer++] = 'L';
			previous = arr[left];
			left++;
		}
		else if (arr[right] > previous) {
			choice[answer++] = 'R';
			previous = arr[right];
			right--;
		}
		else {
			break;
		}
	}

	return answer;
}


#include "stdio.h"
#define SIZE 200001

int n;
int arr[SIZE];
char choice[SIZE];
int calculate();

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}
	int result = calculate();
	printf("%d\n%s", result, choice);
	return 0;
}

int calculate() {
	int left = 0, right = n - 1;
	int answer = 0;
	int previous = 0;

	while (left <= right) {
		if (arr[left] > previous && (arr[left] < arr[right] || arr[right] < previous)) {
			choice[answer++] = 'L';
			previous = arr[left];
			left++;
		}
		else if (arr[right] > previous) {
			choice[answer++] = 'R';
			previous = arr[right];
			right--;
		}
		else {
			break;
		}
	}

	return answer;
}


#include <stdio.h>
#include <string.h>
int num[20005];
char s[20005];
int main () {
    int n, a = 0, b, ans = 1;
    scanf ("%d", &n);
    b = n - 1;
    for (int i = 0; i < n; i ++) {
        scanf ("%d", num + i);
    }
    int tmp;
    if (num[a] < num[b]) {
        tmp = num[a];
        a ++;
        s[0] = 'L';
    } else {
        tmp = num[b];
        b --;
        s[0] = 'R';
    }
    while (1) {
        if (num[a] < num[b]) {
            if (tmp < num[a]) {
                tmp = num[a];
                a ++;
                s[ans ++] = 'L';
            } else if (tmp < num[b]) {
                tmp = num[b];
                b --;
                s[ans ++] = 'R';
            } else {
                break;
            }
        } else {
            if (tmp < num[b]) {
                tmp = num[b];
                b --;
                s[ans ++] = 'R';
            } else if (tmp < num[a]) {
                tmp = num[a];
                a ++;
                s[ans ++] = 'L';
            } else {
                break;
            }
        }
    }
    s[ans] = '\0';
    printf ("%d\n%s", ans, s);
    return 0;
}


#include <stdio.h>
#include <string.h>
int num[20005];
char s[20005];
int main () {
    int n, a = 0, b, ans = 1;
    scanf ("%d", &n);
    b = n - 1;
    for (int i = 0; i < n; i ++) {
        scanf ("%d", num + i);
    }
    int tmp;
    if (num[a] < num[b]) {
        tmp = num[a];
        a ++;
        s[0] = 'L';
    } else {
        tmp = num[b];
        b --;
        s[0] = 'L';
    }
    while (1) {
        if (num[a] < num[b]) {
            if (tmp < num[a]) {
                tmp = num[a];
                a ++;
                s[ans ++] = 'L';
            } else if (tmp < num[b]) {
                tmp = num[b];
                b --;
                s[ans ++] = 'R';
            } else {
                break;
            }
        } else {
            if (tmp < num[b]) {
                tmp = num[b];
                b --;
                s[ans ++] = 'R';
            } else if (tmp < num[a]) {
                tmp = num[a];
                a ++;
                s[ans ++] = 'L';
            } else {
                break;
            }
        }
    }
    s[ans] = '\0';
    printf ("%d\n%s", ans, s);
    return 0;
}


#include <stdio.h>
#include <string.h>
int num[200005];
char s[200005];
int main () {
    int n, a = 0, b, ans = 1;
    scanf ("%d", &n);
    b = n - 1;
    for (int i = 0; i < n; i ++) {
        scanf ("%d", num + i);
    }
    int tmp;
    if (num[a] < num[b]) {
        tmp = num[a];
        a ++;
        s[0] = 'L';
    } else {
        tmp = num[b];
        b --;
        s[0] = 'R';
    }
    while (1) {
        if (num[a] < num[b]) {
            if (tmp < num[a]) {
                tmp = num[a];
                a ++;
                s[ans ++] = 'L';
            } else if (tmp < num[b]) {
                tmp = num[b];
                b --;
                s[ans ++] = 'R';
            } else {
                break;
            }
        } else {
            if (tmp < num[b]) {
                tmp = num[b];
                b --;
                s[ans ++] = 'R';
            } else if (tmp < num[a]) {
                tmp = num[a];
                a ++;
                s[ans ++] = 'L';
            } else {
                break;
            }
        }
    }
    s[ans] = '\0';
    printf ("%d\n%s", ans, s);
    return 0;
}


#include <stdio.h>

int main(void) {
	long int n;
	scanf(" %ld",&n);
	long int a[n];
	for(long int i=0;i<n;i++){
	    scanf(" %ld",&a[i]);
	}

	long int i=0,j=0;
	long int temp[n];
	char final[n];

	if(a[0]<=a[n-1])
	{

	    temp[0]=a[0];
	    final[0]='L';
	   i=1;
	}
	else
	{
	    temp[0]=a[n-1];
	    final[0]='R';
	    j=1;
	}

	while(temp[i+j-1]<a[i]||temp[i+j-1]<a[n-1-j])
	{
	    if(temp[i+j-1]<a[i]&&temp[i+j-1]<a[n-1-j])
	    {
	        if(a[i]>=a[n-1-j])
	        {
	            temp[i+j]=a[n-1-j];
	            final[i+j]='R';
	            j++;
	        }
	        else
	        {
	            temp[i+j]=a[i];
	            final[i+j]='L';
	            i++;
	        }
	    }

	    else if(temp[i+j-1]<a[i])
	    {
	        temp[i+j]=a[i];
	        final[i+j]='L';
	        i++;
	    }
	    else if(temp[i+j-1]<a[n-1-j])
	    {
	        temp[i+j]=a[n-1-j];
	        final[i+j]='R';
	        j++;
	    }
	}
	printf("%ld\n",i+j);
	for(int k=0;k<i+j;k++)
	{
	    printf("%c",final[k]);
	}
	return 0;

}


#include <stdio.h>

int main(void) {
	long int n;
	scanf(" %ld",&n);
	long int a[n];
	for(long int i=0;i<n;i++){
	    scanf(" %ld",&a[i]);
	}

	long int i=0,j=0;
	long int temp[n];
	char final[n];

	if(a[0]<=a[n-1])
	{

	    temp[0]=a[0];
	    final[0]='L';
	   i=1;
	}
	else
	{
	    temp[0]=a[n-1];
	    final[0]='R';
	    j=1;
	}

	while(temp[i+j-1]<a[i]||temp[i+j-1]<a[n-1-j])
	{
	    if(temp[i+j-1]<a[i]&&temp[i+j-1]<a[n-1-j])
	    {
	        if(a[i]>=a[n-1-j])
	        {
	            temp[i+j]=a[n-1-j];
	            final[i+j]='R';
	            j++;
	        }
	        else
	        {
	            temp[i+j]=a[i];
	            final[i+j]='L';
	            i++;
	        }
	    }

	    else if(temp[i+j-1]<a[i])
	    {
	        temp[i+j]=a[i];
	        final[i+j]='L';
	        i++;
	    }
	    else if(temp[i+j-1]<a[n-1-j])
	    {
	        temp[i+j]=a[n-1-j];
	        final[i+j]='R';
	        j++;
	    }
	}
	printf("%ld\n",i+j);
	for(long int k=0;k<i+j;k++)
	{
	    printf("%c",final[k]);
	}
	return 0;

}


#include <stdio.h>

int main(void) {
	long int n;
	scanf(" %ld",&n);
	long int a[n];
	for(long int i=0;i<n;i++){
	    scanf(" %ld",&a[i]);
	}

	long int i=0,j=0;
	long int temp[n];
	char final[n];
	if(n!=1){
	if(a[0]<=a[n-1])
	{

	    temp[0]=a[0];
	    final[0]='L';
	   i=1;
	}
	else
	{
	    temp[0]=a[n-1];
	    final[0]='R';
	    j=1;
	}

	while(temp[i+j-1]<a[i]||temp[i+j-1]<a[n-1-j])
	{
	    if(i+j+1==n)
	    {
	        final[i+j]='L';
	        i++;
	        break;
	    }
	    if(temp[i+j-1]<a[i]&&temp[i+j-1]<a[n-1-j])
	    {
	        if(a[i]>a[n-1-j])
	        {
	            temp[i+j]=a[n-1-j];
	            final[i+j]='R';
	            j++;
	        }
	        else if(a[i]<a[n-1-j])
	        {
	            temp[i+j]=a[i];
	            final[i+j]='L';
	            i++;	        }
	    }

	    else if(temp[i+j-1]<a[i])
	    {
	        temp[i+j]=a[i];
	        final[i+j]='L';
	        i++;
	    }
	    else if(temp[i+j-1]<a[n-1-j])
	    {
	        temp[i+j]=a[n-1-j];
	        final[i+j]='R';
	        j++;
	    }
	}
	printf("%ld\n",i+j);
	for(long int k=0;k<i+j;k++)
	{
	    printf("%c",final[k]);
	}
	}
	else
	{
	    printf("1\n");
	    printf("%c",'L');
	}
	return 0;

}


#include<stdio.h>
#include<stdlib.h>

int main(){

    int size;
    scanf("%d",&size);
    int *array=(int*)malloc(sizeof(int)*size);
    for(int i=0;i<size;i++){
        scanf("%d",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*size);
    int result=0;
    int low=1,high=size-1,last_inserted_element_index=0;
    movements[result++]='L';
    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result++]='R';
    }

    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{
            movements[result]='\0';
            break;
        }
        result++;
    }

    printf("%d\n%s",result,movements);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){

    long int size;
    scanf("%ld",&size);
    long int *array=(long int*)malloc(sizeof(long int)*size);
    for(long int i=0;i<size;i++){
        scanf("%ld",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*size);
    long int result=0;
    long int low=1,high=size-1,last_inserted_element_index=0;
    movements[result]='L';

    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result]='R';
    }
    result++;
    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{

            break;
        }
        result++;
    }
    movements[result]='\0';
    printf("%ld\n%s",result,movements);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){

    int size;
    scanf("%d",&size);
    int *array=(int*)malloc(sizeof(int)*size);
    for(int i=0;i<size;i++){
        scanf("%d",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*size);
    int result=0;
    int low=1,high=size-1,last_inserted_element_index=0;
    movements[result]='L';
    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result]='R';
    }
    result++;
    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{
            break;
        }
        result++;
    }
    movements[result]='\0';
    printf("%d\n%s",result,movements);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){

    long int size;
    scanf("%ld",&size);
    long int *array=(long int*)malloc(sizeof(long int)*size);
    for(long int i=0;i<size;i++){
        scanf("%ld",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*(size+1));
    long int result=0;
    long int low=1,high=size-1,last_inserted_element_index=0;
    movements[result]='L';

    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result]='R';
    }
    result++;
    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{

            break;
        }
        result++;
    }
    movements[result]='\0';
    printf("%ld\n%s",result,movements);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){

    int size;
    scanf("%d",&size);
    int *array=(int*)malloc(sizeof(int)*size);
    for(int i=0;i<size;i++){
        scanf("%d",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*size);
    int result=0;
    int low=1,high=size-1,last_inserted_element_index=0;
    movements[result++]='L';
    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result++]='R';
    }

    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{
            break;
        }
        result++;
    }
    movements[result]='\0';
    printf("%d\n%s",result,movements);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){

    int size;
    scanf("%d",&size);
    int *array=(int*)malloc(sizeof(int)*size);
    for(int i=0;i<size;i++){
        scanf("%d",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*size);
    int result=0;
    int low=1,high=size-1,last_inserted_element_index=0;
    movements[result]='L';

    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result]='R';
    }
    result++;
    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{

            break;
        }
        result++;
    }
    movements[result]='\0';
    printf("%d\n%s",result,movements);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){

    int size;
    scanf("%d",&size);
    int *array=(int*)malloc(sizeof(int)*size);
    for(int i=0;i<size;i++){
        scanf("%d",&array[i]);
    }
    char *movements=(char *)malloc(sizeof(char)*size);
    int result=0;
    int low=1,high=size-1,last_inserted_element_index=0;
    movements[result++]='L';
    if(array[0]>array[size-1]){
        last_inserted_element_index=size-1;
        low=0;
        high--;
        movements[result]='R';
    }

    while(low<=high){
        if(array[low]>array[last_inserted_element_index]&&array[high]>array[last_inserted_element_index]){
            if(array[low]>array[high]){
                last_inserted_element_index=high;
                high--;
                movements[result]='R';
            }
            else{
                last_inserted_element_index=low;
                low++;
                movements[result]='L';
            }
        }
        else if(array[low]>array[last_inserted_element_index]){
            last_inserted_element_index=low;
            low++;
            movements[result]='L';
        }
        else if(array[high]>array[last_inserted_element_index]){
            last_inserted_element_index=high;
            high--;
            movements[result]='R';
        }
        else{
            break;
        }
        result++;
    }
    movements[result]='\0';
    printf("%d\n%s",result,movements);
    return 0;
}


#include<stdio.h>

int LEK[200001];
int min = 0;
char ANS[200001];
int ansi;

int main(){
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&LEK[i]);
    int f=0,b=n-1;
    while(LEK[f]>min||LEK[b]>min){
        if(LEK[f]<LEK[b]){
            if(LEK[f]<min){
                min=LEK[b];
                b--;
                ANS[ansi]='R';ansi++;
            }
            else{
              min=LEK[f];
              f++;ANS[ansi]='L';ansi++;
            }
        }
        else{
          if(LEK[b]<min){
              min=LEK[f];
              f++;ANS[ansi]='L';ansi++;
          }
          else{
            min=LEK[b];
            b--;ANS[ansi]='R';ansi++;
          }
        }
    }
    printf("%d\n%s",ansi,ANS);
}


#include<stdio.h>

int LEK[200001];
int min = 0;

int main(){
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&LEK[i]);
    int f=0,b=n-1;
    while(LEK[f]>min||LEK[b]>min){
        if(LEK[f]<LEK[b]){
            if(LEK[f]<min){
                min=LEK[b];
                b--;printf("R");
            }
            else{
              min=LEK[f];
              f++;printf("L");
            }
        }
        else{
          if(LEK[b]<min){
              min=LEK[f];
              f++;printf("L");
          }
          else{
            min=LEK[b];
            b--;printf("R");
          }
        }
    }
}


#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
    int num;
    scanf("%d",&num);
    char b[num];
    int a[num],c[num];
    for(int i=0;i<num;i++)
        scanf("%d",&a[i]);
    int e=0,d=num-1;
    if(a[e]<=a[d])
    {
        c[0]=a[e];
        b[0]='L';
    }
    else
    {
        c[0]=a[d];
        b[0]='R';
    }
    e=1;
    int count=1;
    for(int i=1;i<num;i++)
    {
        if(a[e]<=a[d])
        {
            if(a[e]<=a[d] && a[e]>c[i-1])
            {

            c[i]=a[e];
            e++;
            count++;
            b[i]='L';
            }
            else if(a[d]>c[i-1])
            {

            c[i]=a[d];
            d--;
            count++;
            b[i]='R';
            }
            else
                break;
        }
        else if(a[e]>a[d])
        {
            if(a[e]>a[d] && a[d]>c[i-1])
            {

            c[i]=a[d];
            d--;
            count++;
            b[i]='R';
            }
            else if(a[e]>c[i-1])
            {

            c[i]=a[e];
            e++;
            count++;
            b[i]='L';
            }
            else
            break;

        }

        else
            break;
    }
    printf("%d\n",count);
    for(int i=0 ;i<num; i++)
        printf("%c",b[i]);
}


#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
    int num;
    scanf("%d",&num);
    char b[num];
    int a[num],c[num];
    for(int i=0;i<num;i++)
        scanf("%d",&a[i]);
    int e=0,d=num-1;
    if(a[e]<=a[d])
    {
        c[0]=a[e];
        b[0]='L';
    }
    else
    {
        c[0]=a[d];
        b[0]='R';
    }
    e=1;
    int count=1;
    for(int i=1;i<num;i++)
    {
        if(a[e]<=a[d])
        {
            if(a[e]<=a[d] && a[e]>c[i-1])
            {

            c[i]=a[e];
            e++;
            count++;
            b[i]='L';
            }
            else if(a[d]>c[i-1])
            {

            c[i]=a[d];
            d--;
            count++;
            b[i]='R';
            }
            else
                break;
        }
        else if(a[e]>a[d])
        {
            if(a[e]>a[d] && a[d]>c[i-1])
            {

            c[i]=a[d];
            d--;
            count++;
            b[i]='R';
            }
            else if(a[e]>c[i-1])
            {

            c[i]=a[e];
            e++;
            count++;
            b[i]='L';
            }
            else
            break;

        }

        else
            break;
    }
    printf("%d\n",count);
    for(int i=0 ;i<count; i++)
        printf("%c",b[i]);
}


#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
    int num;
    scanf("%d",&num);
    char b[num];
    int a[num],c[num];
    for(int i=0;i<num;i++)
        scanf("%d",&a[i]);
    int e=0,d=num-1;
    if(a[e]<=a[d])
    {
        c[0]=a[e];
        b[0]='L';
        e++;
    }
    else
    {
        c[0]=a[d];
        b[0]='R';
        d--;
    }

    int count=1;
    for(int i=1;i<num;i++)
    {
        if(a[e]<=a[d])
        {
            if(a[e]<=a[d] && a[e]>c[i-1])
            {

            c[i]=a[e];
            e++;
            count++;
            b[i]='L';
            }
            else if(a[d]>c[i-1])
            {

            c[i]=a[d];
            d--;
            count++;
            b[i]='R';
            }
            else
                break;
        }
        else if(a[e]>a[d])
        {
            if(a[e]>a[d] && a[d]>c[i-1])
            {

            c[i]=a[d];
            d--;
            count++;
            b[i]='R';
            }
            else if(a[e]>c[i-1])
            {

            c[i]=a[e];
            e++;
            count++;
            b[i]='L';
            }
            else
            break;

        }

        else
            break;
    }
    printf("%d\n",count);
    for(int i=0 ;i<count; i++)
        printf("%c",b[i]);
}


#include <stdio.h>
#include <stdlib.h>

int arr[200000];
int str[200000];

int main(void){
	int i, j, n, N, max=0, cnt=0;

	scanf("%d", &n);

	for(i=0; i<n; i++){
		scanf("%d", &arr[i]);
	}

	i=j=0;
	N=n;
	while(N){
		if(max>arr[i] && max>arr[n-j-1]){
			break;
		}

		if(arr[i]<arr[n-j-1]){
			if(arr[i]>max){
				max=arr[i];
				str[n-N]='L';
				i++;
				cnt++;
			}
			else{
				if(arr[n-j-1]>max){
					max=arr[n-j-1];
					str[n-N]='R';
					j++;
					cnt++;
					N--;
					continue;
				}
				else{
					break;
				}
			}
		}
		else{
			if(arr[n-j-1]>max){
				max=arr[n-j-1];
				str[n-N]='R';
				j++;
				cnt++;
			}
			else{
				if(arr[i]>max){
					max=arr[i];
					str[n-N]='L';
					i++;
					cnt++;
					N--;
					continue;
				}
				else{
					break;
				}
			}
		}
		N--;
	}

	printf("%d\n", cnt);
	for(i=0; i<cnt; i++){
		printf("%c", str[i]);
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int64_t i64;
typedef int32_t i32;

static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}}
static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;}

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

void run (void) {
  i32 n = read_int();
  i32 *a = (i32 *) calloc (n, sizeof (i32));
  for (i32 i = 0; i < n; ++i) {
    a[i] = read_int();
  }
  char *ans = (char *) calloc (n + 1, sizeof (char));
  i32 len = 0;
  i32 prev = 0;
  i32 l = 0;
  i32 r = n - 1;
  while (l <= r) {
    i32 v = MIN(a[l], a[r]);
    if (prev > v) {
      v = MAX(a[l], a[r]);
      if (prev > v) {
	break;
      }
    }
    prev = v;
    if (a[l] == v) {
      l++;
      ans[len++] = 'L';
    } else {
      r--;
      ans[len++] = 'R';
    }
  }
  print_int (len);
  puts ("");
  puts (ans);
}

int main (void) {
  run ();
  return 0;
}


#include <stdio.h>

int main()
{
    int n;
    int a[200001];
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    int i = 0, j = n - 1;
    char ans[200001];
    int size = 0;
    int last = 0;
    while(i <= j) {
        if(a[i] > last && a[j] > last) {
            if(a[i] <= a[j]) {
                last = a[i];
                ans[size++] = 'L';
                ++i;
            } else {
                last = a[j];
                ans[size++] = 'R';
                --j;
            }
        } else if(a[i] > last) {
            last = a[i];
            ans[size++] = 'L';
            ++i;
        } else if(a[j] > last) {
            last = a[j];
            ans[size++] = 'R';
            --j;
        } else {
            break;
        }
    }
    ans[size] = '\0';
    printf("%d\n%s\n", size, ans);
    return 0;
}


#include<stdio.h>

int main(){
	int siz;
	scanf(" %d",&siz);
	int ar[siz];
	for(int i = 0; i < siz;++i){
		scanf(" %d",&ar[i]);
	}
	int i = 0, j = siz -1;
	int min = -1;
	int st = 0;
	char answer[200001];
	while(i <= j){
		if(ar[i] <= min && ar[j] <= min)
			break;
		if(ar[i] < ar[j] && ar[i] > min){
			min = ar[i];
			++i;
			answer[st] = 'L';
			++st;
		}
		else if(ar[j] > min){
			min = ar[j];
			--j;
			answer[st] = 'R';
			++st;
		}
	}
	answer[st] = '\0';
	printf("%d\n",st);
	printf("%s\n",answer);
	return 0;
}


#include<stdio.h>

int main(){
	int siz;
	scanf(" %d",&siz);
	int ar[siz];
	for(int i = 0; i < siz;++i){
		scanf(" %d",&ar[i]);
	}
	int i = 0, j = siz -1;
	int min = -1;
	int st = 0;
	char answer[200001];
	while(i <= j){
		if(ar[i] <= min && ar[j] <= min)
			break;
		if(ar[i] < ar[j] && ar[i] > min){
			min = ar[i];
			++i;
			answer[st] = 'L';
			++st;
		}
		else{
			min = ar[j];
			--j;
			answer[st] = 'R';
			++st;
		}
	}
	answer[st] = '\0';
	printf("%d\n",st);
	printf("%s\n",answer);
	return 0;
}


#include<stdio.h>

int main(){
	int siz;
	scanf(" %d",&siz);
	int ar[siz];
	for(int i = 0; i < siz;++i){
		scanf(" %d",&ar[i]);
	}
	int i = 0, j = siz -1;
	int min = -1;
	int st = 0;
	char answer[200001];
	while(i <= j){
		if(ar[i] <= min && ar[j] <= min)
			break;
		if(ar[i] < ar[j] && ar[i] > min){
			min = ar[i];
			++i;
			answer[st] = 'L';
			++st;
		}
		else(ar[j] > min){
			min = ar[j];
			--j;
			answer[st] = 'R';
			++st;
		}
	}
	answer[st] = '\0';
	printf("%d\n",st);
	printf("%s\n",answer);
	return 0;
}


#include<stdio.h>

int main(){
	int siz;
	scanf(" %d",&siz);
	int ar[siz];
	for(int i = 0; i < siz;++i){
		scanf(" %d",&ar[i]);
	}
	int i = 0, j = siz -1;
	int min = -1;
	int st = 0;
	char answer[200001];
	while(i <= j){
		if(ar[i] <= min && ar[j] <= min)
			break;
		if(ar[i] < ar[j] && ar[i] > min){
			min = ar[i];
			++i;
			answer[st] = 'L';
			++st;
		}
		else if(ar[j] > min){
			min = ar[j];
			--j;
			answer[st] = 'R';
			++st;
		}
		else
			break;
	}
	answer[st] = '\0';
	printf("%d\n",st);
	printf("%s\n",answer);
	return 0;
}


#include<stdio.h>

int main(){
	int siz;
	scanf(" %d",&siz);
	int ar[siz];
	for(int i = 0; i < siz;++i){
		scanf(" %d",&ar[i]);
	}
	int i = 0, j = siz -1;
	int min = -1;
	int st = 0;
	char answer[200001];
	while(i <= j){
		if(ar[i] <= min && ar[j] <= min)
			break;
		else if((ar[i] < ar[j] && ar[i] > min) ||(ar[j] < min && ar[i] > min)){
			min = ar[i];
			++i;
			answer[st] = 'L';
			++st;
		}
		else if(ar[j] > min){
			min = ar[j];
			--j;
			answer[st] = 'R';
			++st;
		}
		else
			break;
	}
	answer[st] = '\0';
	printf("%d\n",st);
	printf("%s\n",answer);
	return 0;
}


#include<stdio.h>
int main()
{
	int n, i, a[200009];
	scanf("%d", &n);
	for(i=0; i<n; i++)
		scanf("%d", a+i);
	int l = 0, r = n-1, p = 0, count = 0;
	char stat[200009];
	while(1)
	{
		if(l == r)
		{
			if(a[l] > p)
				{	stat[count] = 'R'; count++;}
			break;
		}
		if((a[l]>p&&a[r]<=p) || (a[l]>p&&a[r]>p&&a[l]<a[r]))
		{
			p = a[l]; l++;
			stat[count] = 'L';
			count++;
		}
		else if((a[r]>p&&a[l]<=p) || (a[r]>p&&a[l]>p&&a[r]<a[l]))
		{
			p = a[r]; r--;
			stat[count] = 'R';
			count++;
		}
		else
		{
			stat[count] = '\0';
			break;
		}
	}
	printf("%d\n%s", count, stat);
	return 0;
 }


#include<stdio.h>
int main()
{
    int n,i,p=0;
    scanf("%d",&n);
    char a[n];
    for(i=0;i<n;i=i+1)
    {
        scanf("%d",&a[i]);
    }
    int j=n-1;
    for(i=0;i<n;i=i+0)
    {
        if(i==j && p<a[i])
        {
            printf("L");
            break;
        }
        else if(i==j && p>a[i])
        {
            break;
        }
        else if(a[i]>a[j] && p<a[j])
        {
            printf("R");
            p=a[j];
            j=j-1;
        }
        else if(a[i]>a[j] && p>a[j] && p<a[i])
        {
            printf("L");
            p=a[i];
            i=i+1;
        }
        else if(a[i]>a[j] && p>a[j] && p>a[i])
        {
            break;
        }
        else if(a[i]<a[j] && p<a[i])
        {
            printf("L");
            p=a[i];
            i=i+1;
        }
        else if(a[i]<a[j] && p>a[i] && p<a[j])
        {
            printf("R");
            p=a[j];
            j=j-1;
        }
        else if(a[i]<a[j] && p>a[j] && p>a[i])
        {
            break;
        }
    }
}


#include <stdio.h>

int main() {
    int n, i, j, k;
    i = k = 0;
    scanf("%d", &n);
    int seq[n];
    char result[n];
    int nums[n];

    for (i = 0; i < n; i++) {
        scanf("%d", &nums[i]);
    }

    i = 0;
    j = n - 1;
    while (i <= j) {
        if (i == j) {
            if (nums[i] > seq[k - 1]) {
                result[k++] = 'L';
                break;
            }
        }
        if (k == 0) {

            if (nums[i] < nums[j]) {
                result[k] = 'L';
                seq[k++] = nums[i];
                i++;
            } else {
                result[k] = 'R';
                seq[k++] = nums[j];
                j--;
            }
        } else {
            if (nums[i] < seq[k - 1]) {
                if (nums[j] < seq[k - 1]) {
                    break;
                } else {
                    result[k] = 'R';
                    seq[k++] = nums[j];
                    j--;
                }
            } else {
                if (nums[j] > seq[k - 1]) {
                    if (nums[i] < nums[j]) {
                        result[k] = 'L';
                        seq[k++] = nums[i];
                        i++;
                    } else {
                        result[k] = 'R';
                        seq[k++] = nums[j];
                        j--;
                    }
                } else {
                    result[k] = 'L';
                    seq[k++] = nums[i];
                    i++;
                }
            }
        }
    }

    printf("%d\n", k);
    for (int i = 0; i < k; i++) {
        putchar(result[i]);
    }

    putchar('\n');
}


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int main()
{
	register bool a = false;
	register bool b = false;

	int size; scanf("%d" , &size);
	int* vetor = malloc(sizeof(int) * size);

	for (int i = 0; i < size; ++i)
		scanf("%d" , &vetor[i]);

	char* lados = malloc(sizeof(char) * 10001);
	register int passos = 0;

	register int ultimo_pop = -1;

	register int comeco = 0;
	register int final = size - 1;

	a = vetor[comeco] > ultimo_pop;
	b = vetor[final]  > ultimo_pop;

	while (comeco != final)
	{
		if (a && b) // Se ambos podem, escolher o menor deles
		{
			if (vetor[comeco] < vetor[final])
			{
				lados[passos++] = 'L';
				ultimo_pop = vetor[comeco++];

				a = vetor[comeco] > ultimo_pop; // Atualizar A
			}
			else
			{
				lados[passos++] = 'R';
				ultimo_pop = vetor[final--];

				b = vetor[final] > ultimo_pop; // Atualizar B
			}
		}

		else if (a)
		{
			lados[passos++] = 'L';
			ultimo_pop = vetor[comeco++];

			a = vetor[comeco] > ultimo_pop; // Atualizar A
		}
		else if (b)
		{
			lados[passos++] = 'R';
			ultimo_pop = vetor[final--];

			b = vetor[final] > ultimo_pop; // Atualizar B
		}
		else
			break;
	}

	if (comeco == final)
	{
		if (vetor[comeco] > ultimo_pop)
		{
			lados[passos++] = 'L'; // Ambíguo
		}
	}

	printf("%d\n" , passos);

	lados[passos] = '\0';
	printf("%s\n" , lados);
}


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int main()
{
	register bool a = false;
	register bool b = false;

	int size; scanf("%d" , &size);
	int* vetor = malloc(sizeof(int) * size);

	for (int i = 0; i < size; ++i)
		scanf("%d" , &vetor[i]);

	char* lados = malloc(sizeof(char) * 20001);
	register int passos = 0;

	register int ultimo_pop = -1;

	register int comeco = 0;
	register int final = size - 1;

	a = vetor[comeco] > ultimo_pop;
	b = vetor[final]  > ultimo_pop;

	while (comeco != final)
	{
		if (a && b) // Se ambos podem, escolher o menor deles
		{
			if (vetor[comeco] < vetor[final])
			{
				lados[passos++] = 'L';
				ultimo_pop = vetor[comeco++];

				a = vetor[comeco] > ultimo_pop; // Atualizar A
			}
			else
			{
				lados[passos++] = 'R';
				ultimo_pop = vetor[final--];

				b = vetor[final] > ultimo_pop; // Atualizar B
			}
		}

		else if (a)
		{
			lados[passos++] = 'L';
			ultimo_pop = vetor[comeco++];

			a = vetor[comeco] > ultimo_pop; // Atualizar A
		}
		else if (b)
		{
			lados[passos++] = 'R';
			ultimo_pop = vetor[final--];

			b = vetor[final] > ultimo_pop; // Atualizar B
		}
		else
			break;
	}

	if (comeco == final)
	{
		if (vetor[comeco] > ultimo_pop)
		{
			lados[passos++] = 'L'; // Ambíguo
		}
	}

	printf("%d\n" , passos);

	lados[passos] = '\0';
	printf("%s\n" , lados);
}


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int main()
{
	register bool a = false;
	register bool b = false;

	int size; scanf("%d" , &size);
	register int* vetor = malloc(sizeof(int) * size);

	for (int i = 0; i < size; ++i)
		scanf("%d" , &vetor[i]);

	register char* lados = malloc(sizeof(char) * 10001);
	register int passos = 0;

	register int ultimo_pop = -1;

	register int comeco = 0;
	register int final = size - 1;

	a = vetor[comeco] > ultimo_pop;
	b = vetor[final]  > ultimo_pop;

	while (comeco != final)
	{
		if (a && b) // Se ambos podem, escolher o menor deles
		{
			if (vetor[comeco] < vetor[final])
			{
				lados[passos++] = 'L';
				ultimo_pop = vetor[comeco++];

				a = vetor[comeco] > ultimo_pop; // Atualizar A
			}
			else
			{
				lados[passos++] = 'R';
				ultimo_pop = vetor[final--];

				b = vetor[final] > ultimo_pop; // Atualizar B
			}
		}

		else if (a)
		{
			lados[passos++] = 'L';
			ultimo_pop = vetor[comeco++];

			a = vetor[comeco] > ultimo_pop; // Atualizar A
		}
		else if (b)
		{
			lados[passos++] = 'R';
			ultimo_pop = vetor[final--];

			b = vetor[final] > ultimo_pop; // Atualizar B
		}
		else
			break;
	}

	if (comeco == final)
	{
		if (vetor[comeco] > ultimo_pop)
		{
			lados[passos++] = 'L'; // Ambíguo
		}
	}

	printf("%d\n" , passos);

	lados[passos] = '\0';
	printf("%s\n" , lados);
}


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int main()
{
	register bool a = false;
	register bool b = false;

	int size; scanf("%d" , &size);
	int* vetor = malloc(sizeof(int) * size);

	for (int i = 0; i < size; ++i)
		scanf("%d" , &vetor[i]);

	char* lados = malloc(sizeof(char) * 200001);
	register int passos = 0;

	register int ultimo_pop = -1;

	register int comeco = 0;
	register int final = size - 1;

	a = vetor[comeco] > ultimo_pop;
	b = vetor[final]  > ultimo_pop;

	while (comeco != final)
	{
		if (a && b) // Se ambos podem, escolher o menor deles
		{
			if (vetor[comeco] < vetor[final])
			{
				lados[passos++] = 'L';
				ultimo_pop = vetor[comeco++];

				a = vetor[comeco] > ultimo_pop; // Atualizar A
			}
			else
			{
				lados[passos++] = 'R';
				ultimo_pop = vetor[final--];

				b = vetor[final] > ultimo_pop; // Atualizar B
			}
		}

		else if (a)
		{
			lados[passos++] = 'L';
			ultimo_pop = vetor[comeco++];

			a = vetor[comeco] > ultimo_pop; // Atualizar A
		}
		else if (b)
		{
			lados[passos++] = 'R';
			ultimo_pop = vetor[final--];

			b = vetor[final] > ultimo_pop; // Atualizar B
		}
		else
			break;
	}

	if (comeco == final)
	{
		if (vetor[comeco] > ultimo_pop)
		{
			lados[passos++] = 'L'; // Ambíguo
		}
	}

	printf("%d\n" , passos);

	lados[passos] = '\0';
	printf("%s\n" , lados);
}


#include<stdio.h>
int num[200005],top=1,topp=1,st1[200005];char st2[200005];
int in1(int a)
{
    st1[top]=a;
    top++;
}
int in2(char a)
{
    st2[topp]=a;
    topp++;
}
int main()
{
    int i,n;char ch;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
    }
    int k=n-1;i=0;
    while(1)
    {
        if(num[i]<num[k])
        {
            if(st1[top-1]<num[i])
            {in1(num[i]);in2('L');i++;}
            else if(st1[top-1]<num[k])
                {in1(num[k]);in2('R');
            k--;}
            else break;
        }
        else
        {
            if(st1[top-1]<num[k])
            {in1(num[k]);in2('R');k--;}
            else if(st1[top-1]<num[i])
                {in1(num[i]);in2('L');
            i++;}
            else break;
        }
        if(i-k==1)
            break;
    }
    printf("%d\n",top-1);
    for(i=1;i<=top;i++)
        {
            printf("%c",st2[i]);
        }

}


#include<stdio.h>
int num[200005],top=1,topp=1,st1[200005];char st2[200005];
int in1(int a)
{
    st1[top]=a;
    top++;
}
int in2(char a)
{
    st2[topp]=a;
    topp++;
}
int main()
{
    int i,n;char ch;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
    }
    int k=n-1;i=0;
    while(1)
    {
        if(num[i]<num[k])
        {
            if(st1[top-1]<num[i])
            {in1(num[i]);in2('L');i++;}
            else if(st1[top-1]<num[k])
                {in1(num[k]);in2('R');
            k--;}
            else break;
        }
        else
        {
            if(st1[top-1]<num[k])
            {in1(num[k]);in2('R');k--;}
            else if(st1[top-1]<num[i])
                {in1(num[i]);in2('L');
            i++;}
            else break;
        }
        if(i-k==1)
            break;
    }
    for(i=1;i<=top;i++)
        {
            printf("%c",st2[i]);
        }

}


#include<stdio.h>
int num[200005],top=1,topp=1,st1[200005];char st2[200005];
int in1(int a)
{
    st1[top]=a;
    top++;
}
int in2(char a)
{
    st2[topp]=a;
    topp++;
}
int main()
{
    int i,n;char ch;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
    }
    int k=n-1;i=0;
    while(1)
    {
        if(num[i]<num[k])
        {
            if(st1[top-1]<num[i])
            {in1(num[i]);in2('L');i++;}
            else if(st1[top-1]<num[k])
                {in1(num[k]);in2('R');
            k--;}
            else break;
        }
        else
        {
            if(st1[top-1]<num[k])
            {in1(num[k]);in2('R');k--;}
            else if(st1[top-1]<num[i])
                {in1(num[i]);in2('L');
            i++;}
            else break;
        }
        if(i-k==1)
            break;
    }
    printf("%d\n",top-1);
    for(i=1;i<top;i++)
        {
            printf("%c",st2[i]);
        }
}


#include<stdio.h>
#define MAXN 200001
int a[MAXN];
char p[MAXN]={0};
int main()
{
    int n;
    int len=0;
    int last_pop=0;
    int l,r;
    int dbg=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",a+i);
    }
    l=0;r=n-1;
    while(l!=(r+1))
    {
        if(a[l]>last_pop)
        {
            if(a[r]>last_pop)
                if(a[l]<a[r])
                {
                    last_pop=a[l];
                    l++;
                    if(dbg)printf("L");
                    p[len++]='L';
                    continue;
                }
                else
                {
                    last_pop=a[r];
                    r--;
                    p[len++]='R';
                    if(dbg)printf("R");
                    continue;
                }
            else
            {
                last_pop=a[l];
                l++;
                p[len++]='L';
                if(dbg)printf("L");
                continue;
            }
        }
        else
            if(a[r]>last_pop)
            {
                last_pop=a[r];
                r--;
                p[len++]='R';
                if(dbg)printf("R");
                continue;
            }
            else
            {
                printf("break");
                break;
            }

    }
    p[len]='\0';
    printf("%d\n%s",len,p);
}


#include<stdio.h>
#define MAXN 200001
int a[MAXN];
char p[MAXN]={0};
int main()
{
    int n;
    int len=0;
    int last_pop=0;
    int l,r;
    int dbg=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",a+i);
    }
    l=0;r=n-1;
    while(l!=(r+1))
    {
        if(a[l]>last_pop)
        {
            if(a[r]>last_pop)
                if(a[l]<a[r])
                {
                    last_pop=a[l];
                    l++;
                    if(dbg)printf("L");
                    p[len++]='L';
                    continue;
                }
                else
                {
                    last_pop=a[r];
                    r--;
                    p[len++]='R';
                    if(dbg)printf("R");
                    continue;
                }
            else
            {
                last_pop=a[l];
                l++;
                p[len++]='L';
                if(dbg)printf("L");
                continue;
            }
        }
        else
            if(a[r]>last_pop)
            {
                last_pop=a[r];
                r--;
                p[len++]='R';
                if(dbg)printf("R");
                continue;
            }
            else
            {
                //printf("break");
                break;
            }

    }
    p[len]='\0';
    printf("%d\n%s",len,p);
}


#include <stdio.h>
int main()
{
    int n,i,l,r;
    int x=0;
    scanf("%d", &n);
    int arr[n];
    char s[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d", &arr[i]);
    }
    for( i=0,l=0,r=n-1;l<r;)
    {
        if(arr[l]<arr[r]&&arr[l]>x)
        {
            s[i]='L';
            x=arr[l];
            l++;
            i++;
        }

        else if(arr[r]<arr[l]&&arr[r]>x)
        {
            s[i]='R';
            x=arr[r];
            r--;
            i++;
        }
        else if(arr[l]>arr[r]&&arr[l]>x)
        {
            s[i]='L';
            x=arr[l];
            l++;
            i++;
        }

        else if(arr[r]>arr[l]&&arr[r]>x)
        {
            s[i]='R';
            x=arr[r];
            r--;
            i++;
        }
        else
        {
            break;
        }
    }
    if(arr[l]>x)
    {
        s[i]='R';
        i++;
    }

    s[i]='\0';
    printf("%d\n%s\n", i, s);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <malloc.h>
int searchLeft(int head, int tail, int curLast, int *arr)
{
	int count = 0;
	while (tail >= head && arr[tail] > curLast)
	{
		curLast = arr[tail];
		tail--;
		count++;
	}
	return count;
}
int searchRight(int head, int tail, int curLast, int *arr)
{
	int count = 0;
	while (head <= tail && arr[head] > curLast)
	{
		curLast = arr[head];
		head++;
		count++;
	}
	return count;
}
void toLeft(int head, int tail, int curLast, int *arr, char *output, int *nOutput)
{
	while (tail >= head && arr[tail] > curLast)
	{
		output[*nOutput] = 'R';
		(*nOutput)++;
		curLast = arr[tail];
		tail--;
	}
}
void toRight(int head, int tail, int curLast, int *arr, char *output, int *nOutput)
{
	while (head <= tail && arr[head] > curLast)
	{
		output[*nOutput] = 'L';
		(*nOutput)++;
		curLast = arr[head];
		head++;
	}
}
int canMoveOn(int head, int tail, int curLast, int *arr)
{
	int result = 0;
	if (arr[head] > curLast)
		result++;
	if (arr[tail] > curLast)
		result++;
	return result;
}
int main()
{
	int n, *arr, head = 0, tail, curLast = 0, temp, difference, nOutput = 0, a;
	char output[200001];
	scanf("%d", &n);
	arr = (int *)(malloc(n * sizeof(int)));
	for (a = 0; a < n; a++)
	{
		scanf("%d", &arr[a]);
	}
	tail = n - 1;
	while (head <= tail)
	{
		temp = canMoveOn(head, tail, curLast, arr);
		if (temp == 2)
		{
			difference = arr[head] - curLast - (arr[tail] - curLast);
			if (difference < 0)
			{
				output[nOutput] = 'L';
				nOutput++;
				curLast = arr[head];
				head++;
			}
			else if (difference == 0)
			{
				if (searchLeft(head, tail, curLast, arr) >= searchRight(head, tail, curLast, arr))
					toLeft(head, tail, curLast, arr, output, &nOutput);
				else
					toRight(head, tail, curLast, arr, output, &nOutput);
				break;
			}
			else
			{
				output[nOutput] = 'R';
				nOutput++;
				curLast = arr[tail];
				tail--;
			}
		}
		else if (temp == 1)
		{
			if (arr[head] > curLast)
				toRight(head, tail, curLast, arr, output, &nOutput);
			else
				toLeft(head, tail, curLast, arr, output, &nOutput);
			break;
		}
		else
			break;
	}
	output[nOutput] = '\0';
	printf("%d\n", nOutput);
	puts(output);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <ctime>

using namespace std;

#define F first
#define S second

int sx, sy, fx, fy, n;

pair <int, pair <int, int> > a[200000];

vector <int> ok[200000], g[200000], cur;

pair <pair <int, int>, int> q[90000000];

bool check(int x, int y) {
	if (x > 1000 * 1000 * 1000 || x < 1)
		return false;
	if (y > 1000 * 1000 * 1000 || y < 1)
		return false;
	if (g[x].size() == 0)
		return false;
	int l = 0;
	int r = g[x].size() - 1;
	while(l < r) {
		int mid = (l + r) / 2;
		if (g[x][mid] < y)
			l = mid + 1;
		else
			r = mid;
	}
	if (g[x][r] == y && !ok[x][r]) {
		ok[x][r] = 1;
		return true;
	}
	return false;
}

int main() {
	cin >> sx >> sy >> fx >> fy;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].F >> a[i].S.F >> a[i].S.S;
	}

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n; i++) {
		if (g[a[i].F].size() == 0) {
			for (int j = a[i].S.F; j <= a[i].S.S; j++) {
				g[a[i].F].push_back(j);
				ok[a[i].F].push_back(0);
			}
		} else
			if (a[i].S.S > g[a[i].F][g[a[i].F].size() - 1]) {
				for (int j = max(g[a[i].F][g[a[i].F].size() - 1], a[i].S.F); j <= a[i].S.S; j++) {
					g[a[i].F].push_back(j);
					ok[a[i].F].push_back(0);
				}
			}
	}
	int l = 0, r = 0;
	q[++r].F.F = sx;
	q[r].F.S = sy;
	q[r].S = 0;
	while (l < r){
		l++;
		if (q[l].F.F == fx && q[l].F.S == fy) {
			cout << q[l].S << endl;
			return 0;
		}
		if (check(q[l].F.F + 1, q[l].F.S - 1)) {
			q[++r].F.F = q[l].F.F + 1;
			q[r].F.S = q[l].F.S - 1;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F + 1, q[l].F.S + 1)) {
	        	q[++r].F.F = q[l].F.F + 1;
			q[r].F.S = q[l].F.S + 1;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F - 1, q[l].F.S - 1)) {
			q[++r].F.F = q[l].F.F - 1;
			q[r].F.S = q[l].F.S - 1;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F - 1, q[l].F.S + 1)) {
	        	q[++r].F.F = q[l].F.F - 1;
			q[r].F.S = q[l].F.S + 1;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F + 1, q[l].F.S)) {
			q[++r].F.F = q[l].F.F + 1;
			q[r].F.S = q[l].F.S;
			q[r].S = q[l].S + 1;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F - 1, q[l].F.S)) {
	        	q[++r].F.F = q[l].F.F - 1;
			q[r].F.S = q[l].F.S;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F, q[l].F.S + 1)) {
			q[++r].F.F = q[l].F.F;
			q[r].F.S = q[l].F.S + 1;
			q[r].S = q[l].S + 1;
		}
		if (check(q[l].F.F, q[l].F.S - 1)) {
			q[++r].F.F = q[l].F.F;
			q[r].F.S = q[l].F.S - 1;
			q[r].S = q[l].S + 1;
		}
	}
	cout << -1;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <ctime>

using namespace std;

#define F first
#define S second

int sx, sy, fx, fy, n;

pair <int, pair <int, int> > a[200000];

vector <int> ok[200000], g[200000], cur;

pair <pair <int, int>, long long> q[4000000];

bool check(int x, int y) {
	if (x > 1000 * 1000 * 1000 || x < 1)
		return false;
	if (y > 1000 * 1000 * 1000 || y < 1)
		return false;
	if (g[x].size() == 0)
		return false;
	int l = 0;
	int r = g[x].size() - 1;
	while(l < r) {
		int mid = (l + r) / 2;
		if (g[x][mid] < y)
			l = mid + 1;
		else
			r = mid;
	}
	if (g[x][r] == y && !ok[x][r]) {
		ok[x][r] = 1;
		return true;
	}
	return false;
}

int main() {
	cin >> sx >> sy >> fx >> fy;
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> a[i].F >> a[i].S.F >> a[i].S.S;
	}

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n; i++) {
		if (g[a[i].F].size() == 0) {
			for (int j = a[i].S.F; j <= a[i].S.S; j++) {
				g[a[i].F].push_back(j);
				ok[a[i].F].push_back(0);
			}
		} else
			if (a[i].S.S > g[a[i].F][g[a[i].F].size() - 1]) {
				for (int j = max(g[a[i].F][g[a[i].F].size() - 1], a[i].S.F); j <= a[i].S.S; j++) {
					g[a[i].F].push_back(j);
					ok[a[i].F].push_back(0);
				}
			}
	}

	int l = 0, r = 0;
	q[++r].F.F = sx;
	q[r].F.S = sy;
	q[r].S = 0;

	while (l < r){
		l++;
		if (q[l].F.F == fx && q[l].F.S == fy) {
			cout << q[l].S << endl;
			return 0;
		}

		if (check(q[l].F.F + 1, q[l].F.S - 1)) {
			q[++r].F.F = q[l].F.F + 1;
			q[r].F.S = q[l].F.S - 1;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F + 1, q[l].F.S + 1)) {
	        	q[++r].F.F = q[l].F.F + 1;
			q[r].F.S = q[l].F.S + 1;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F - 1, q[l].F.S - 1)) {
			q[++r].F.F = q[l].F.F - 1;
			q[r].F.S = q[l].F.S - 1;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F - 1, q[l].F.S + 1)) {
	        	q[++r].F.F = q[l].F.F - 1;
			q[r].F.S = q[l].F.S + 1;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F + 1, q[l].F.S)) {
			q[++r].F.F = q[l].F.F + 1;
			q[r].F.S = q[l].F.S;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F - 1, q[l].F.S)) {
	        	q[++r].F.F = q[l].F.F - 1;
			q[r].F.S = q[l].F.S;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F, q[l].F.S + 1)) {
			q[++r].F.F = q[l].F.F;
			q[r].F.S = q[l].F.S + 1;
			q[r].S = q[l].S + 1ll;
		}

		if (check(q[l].F.F, q[l].F.S - 1)) {
			q[++r].F.F = q[l].F.F;
			q[r].F.S = q[l].F.S - 1;
			q[r].S = q[l].S + 1ll;
		}
	}
	cout << -1;
	return 0;
}


#include<iostream>
#include<map>
#include<stdio.h>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
ll x[]={-1,-1,-1,0,0,1,1,1},y[]={-1,0,1,-1,1,-1,0,1};
int main(){
ll x0,y0,x1,y1;
scanf("%lld%lld%lld%lld",&x0,&y0,&x1,&y1);
ll m;
scanf("%lld",&m);
map<p,ll>path;
ll r,c1,c2;
while(m--){
scanf("%lld%lld%lld",&r,&c1,&c2);
for(int i=c1;i<=c2;i++){
	path[mp(r,i)]=-1;
}
}
path[mp(x0,y0)]=0;
queue<p>bfs;
bfs.push(mp(x0,y0));
p t;
while(bfs.size()){
	t=bfs.front();
	bfs.pop();
	for(int i=0;i<8;i++){
		if(path[mp(t.f+x[i],t.s+y[i])]<0){
		path[mp(t.f+x[i],t.s+y[i])]=path[t]+1;
		bfs.push(mp(t.f+x[i],t.s+y[i]));
		}
	}
}
cout<<path[mp(x1,y1)];
}


#include<iostream>
#include<map>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
struct pro{
ll x,y,val
pro();
pro(ll xi,ll yi,ll vali){
x=xi;y=yi;val=vali;
}
};
int main(){
ll x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
map<p,bool>vis;
ll m;
cin>>m;
map<ll,p>path;
ll r,c1,c2;
while(m--){
cin>>r>>c1>>c2;
path[r]=make_pair(c1,c2);
}
pro t=pro(x0,y0,0);
queue<pro>bfs;
bfs.push(t);
p temp;
while(bfs.size()){
t=bfs.front();
bfs.pop();
vis[t]=1;
if(t.x==x1&&t.y==y1){
cout<<t.val;
return 0;
}
if(t.x<0||t.x>1000000000||t.y<0||t.y>1000000000||(path[t.x].f<=t.y&&path[t.x].s>=t.y)) continue;
//temp=path[t.x];
if(vis[mp(t.x,t.y+1)]==0){
bfs.push(pro(t.x,t.y+1,t.val+1));
}
if(vis[mp(t.x,t.y-1)]==0){
bfs.push(pro(t.x,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y)]==0){
bfs.push(pro(t.x+1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y)]==0){
bfs.push(pro(t.x-1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y-1)]==0){
bfs.push(pro(t.x-1,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y+1)]==0){
bfs.push(pro(t.x+1,t.y+1,t.val+1));
}
if(vis[mp(t.x+1,t.y-1)]==0){
bfs.push(pro(t.x+1,t.y-1,t.val+1));
}
if(vis[mp(t.x-1,t.y+1)]==0){
bfs.push(pro(t.x-1,t.y+1,t.val+1));
}
}
}


#include<iostream>
#include<map>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
struct pro{
ll x,y,val;
pro();
pro(ll xi,ll yi,ll vali){
x=xi;y=yi;val=vali;
}
};
int main(){
ll x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
map<p,bool>vis;
ll m;
cin>>m;
map<ll,p>path;
ll r,c1,c2;
while(m--){
cin>>r>>c1>>c2;
for(int i=c1;i<=c2;i++){
	path[r]=mp(c1,c2);
}
}
pro t=pro(x0,y0,0);
queue<pro>bfs;
bfs.push(t);
p temp;
while(bfs.size()){
t=bfs.front();
bfs.pop();
vis[mp(t.x,t.y)]=1;
if(t.x==x1&&t.y==y1){
cout<<t.val;
return 0;
}
if(t.x<0||t.x>1000000000||t.y<0||t.y>1000000000||path[t.x].f>t.y||path[t.x].s<t.y) continue;
//temp=path[t.x];
if(vis[mp(t.x,t.y+1)]==0){
bfs.push(pro(t.x,t.y+1,t.val+1));
}
if(vis[mp(t.x,t.y-1)]==0){
bfs.push(pro(t.x,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y)]==0){
bfs.push(pro(t.x+1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y)]==0){
bfs.push(pro(t.x-1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y-1)]==0){
bfs.push(pro(t.x-1,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y+1)]==0){
bfs.push(pro(t.x+1,t.y+1,t.val+1));
}
if(vis[mp(t.x+1,t.y-1)]==0){
bfs.push(pro(t.x+1,t.y-1,t.val+1));
}
if(vis[mp(t.x-1,t.y+1)]==0){
bfs.push(pro(t.x-1,t.y+1,t.val+1));
}
}
cout<<-1;
}


#include<iostream>
#include<map>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
struct pro{
ll x,y,val;
pro();
pro(ll xi,ll yi,ll vali){
x=xi;y=yi;val=vali;
}
};
int main(){
ll x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
map<p,bool>vis;
ll m;
cin>>m;
map<ll,p>path;
ll r,c1,c2;
while(m--){
cin>>r>>c1>>c2;
path[r]=mp(c1,c2);
}
pro t=pro(x0,y0,0);
queue<pro>bfs;
bfs.push(t);
p temp;
while(bfs.size()){
t=bfs.front();
bfs.pop();
vis[mp(t.x,t.y)]=1;
if(t.x==x1&&t.y==y1){
cout<<t.val;
return 0;
}
if(t.x<0||t.x>1000000000||t.y<0||t.y>1000000000) continue;
//if(!(path[t.x].f<=t.y&&path[t.x].s>=t.y)) continue;
//temp=path[t.x];
if(vis[mp(t.x,t.y+1)]==0){
bfs.push(pro(t.x,t.y+1,t.val+1));
}
if(vis[mp(t.x,t.y-1)]==0){
bfs.push(pro(t.x,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y)]==0){
bfs.push(pro(t.x+1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y)]==0){
bfs.push(pro(t.x-1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y-1)]==0){
bfs.push(pro(t.x-1,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y+1)]==0){
bfs.push(pro(t.x+1,t.y+1,t.val+1));
}
if(vis[mp(t.x+1,t.y-1)]==0){
bfs.push(pro(t.x+1,t.y-1,t.val+1));
}
if(vis[mp(t.x-1,t.y+1)]==0){
bfs.push(pro(t.x-1,t.y+1,t.val+1));
}
}
cout<<-1;
}


#include<iostream>
#include<map>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
struct pro{
ll x,y,val;
pro();
pro(ll xi,ll yi,ll vali){
x=xi;y=yi;val=vali;
}
};
int main(){
ll x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
map<p,bool>vis;
ll m;
cin>>m;
map<p,bool>path;
ll r,c1,c2;
while(m--){
cin>>r>>c1>>c2;
for(int i=c1;i<=c2;i++){
	path[mp(r,i)]=1;
}
}
pro t=pro(x0,y0,0);
queue<pro>bfs;
bfs.push(t);
p temp;
while(bfs.size()){
t=bfs.front();
bfs.pop();
vis[mp(t.x,t.y)]=1;
if(t.x==x1&&t.y==y1){
cout<<t.val;
return 0;
}
if(t.x<0||t.x>1000000000||t.y<0||t.y>1000000000||path[mp(t.x,t.y)]==0) continue;
//temp=path[t.x];
if(vis[mp(t.x,t.y+1)]==0){
bfs.push(pro(t.x,t.y+1,t.val+1));
}
if(vis[mp(t.x,t.y-1)]==0){
bfs.push(pro(t.x,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y)]==0){
bfs.push(pro(t.x+1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y)]==0){
bfs.push(pro(t.x-1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y-1)]==0){
bfs.push(pro(t.x-1,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y+1)]==0){
bfs.push(pro(t.x+1,t.y+1,t.val+1));
}
if(vis[mp(t.x+1,t.y-1)]==0){
bfs.push(pro(t.x+1,t.y-1,t.val+1));
}
if(vis[mp(t.x-1,t.y+1)]==0){
bfs.push(pro(t.x-1,t.y+1,t.val+1));
}
}
cout<<-1;
}


#include<iostream>
#include<map>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
struct pro{
ll x,y,val;
pro();
pro(ll xi,ll yi,ll vali){
x=xi;y=yi;val=vali;
}
};
int main(){
ll x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
map<p,bool>vis;
ll m;
cin>>m;
map<ll,p>path;
ll r,c1,c2;
while(m--){
cin>>r>>c1>>c2;
path[r]=mp(c1,c2);
}
pro t=pro(x0,y0,0);
queue<pro>bfs;
bfs.push(t);
p temp;
while(bfs.size()){
t=bfs.front();
bfs.pop();
vis[mp(t.x,t.y)]=1;
if(t.x==x1&&t.y==y1){
cout<<t.val;
return 0;
}
if(t.x<0||t.x>1000000000||t.y<0||t.y>1000000000) continue;
if((path[t.x].f<=t.y&&path[t.x].s>=t.y)) continue;
//temp=path[t.x];
if(vis[mp(t.x,t.y+1)]==0){
bfs.push(pro(t.x,t.y+1,t.val+1));
}
if(vis[mp(t.x,t.y-1)]==0){
bfs.push(pro(t.x,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y)]==0){
bfs.push(pro(t.x+1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y)]==0){
bfs.push(pro(t.x-1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y-1)]==0){
bfs.push(pro(t.x-1,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y+1)]==0){
bfs.push(pro(t.x+1,t.y+1,t.val+1));
}
if(vis[mp(t.x+1,t.y-1)]==0){
bfs.push(pro(t.x+1,t.y-1,t.val+1));
}
if(vis[mp(t.x-1,t.y+1)]==0){
bfs.push(pro(t.x-1,t.y+1,t.val+1));
}
}
cout<<-1;
}


#include<iostream>
#include<map>
#include<stdio.h>
#include<queue>
using namespace std;
#define ll long long int
#define p pair<ll,ll>
#define f first
#define s second
#define mp make_pair
struct pro{
ll x,y,val;
pro();
pro(ll xi,ll yi,ll vali){
x=xi;y=yi;val=vali;
}
};
int main(){
ll x0,y0,x1,y1;
scanf("%lld%lld%lld%lld",&x0,&y0,&x1,&y1);
map<p,bool>vis;
ll m;
scanf("%lld",&m);
map<p,bool>path;
ll r,c1,c2;
while(m--){
scanf("%lld%lld%lld",&r,&c1,&c2);
for(int i=c1;i<=c2;i++){
	path[mp(r,i)]=1;
}
}
pro t=pro(x0,y0,0);
queue<pro>bfs;
bfs.push(t);
p temp;
while(bfs.size()){
t=bfs.front();
bfs.pop();
vis[mp(t.x,t.y)]=1;
if(t.x==x1&&t.y==y1){
cout<<t.val;
return 0;
}
if(t.x<0||t.x>1000000000||t.y<0||t.y>1000000000) continue;
if(path[mp(t.x,t.y)]==0) continue;
if(vis[mp(t.x,t.y+1)]==0){
bfs.push(pro(t.x,t.y+1,t.val+1));
}
if(vis[mp(t.x,t.y-1)]==0){
bfs.push(pro(t.x,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y)]==0){
bfs.push(pro(t.x+1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y)]==0){
bfs.push(pro(t.x-1,t.y,t.val+1));
}
if(vis[mp(t.x-1,t.y-1)]==0){
bfs.push(pro(t.x-1,t.y-1,t.val+1));
}
if(vis[mp(t.x+1,t.y+1)]==0){
bfs.push(pro(t.x+1,t.y+1,t.val+1));
}
if(vis[mp(t.x+1,t.y-1)]==0){
bfs.push(pro(t.x+1,t.y-1,t.val+1));
}
if(vis[mp(t.x-1,t.y+1)]==0){
bfs.push(pro(t.x-1,t.y+1,t.val+1));
}
}
cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
typedef long long ll;
typedef unsigned long long ull;
int sx,sy,ex,ey;
map<pair<int,int>,int>m;
queue<pair<int,int> >q;
void bfs()
{
        m[pair<int,int>(sx,sy)]=0;
        q.push(mk(sx,sy));

        while(!q.empty())
        {
                int i=q.front().first;
                int j=q.front().second;
                q.pop();
                if(i==ex&&j==ey)
                return ;
                if(j+1<=1000&&m[pair<int,int>(i,j+1)]==-1)
                {
                        m[pair<int,int>(i,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j+1));
                }
                if(j-1>=0&&m[pair<int,int>(i,j-1)]==-1)
                {
                        m[pair<int,int>(i,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j-1));
                }
                if(i+1<=1000&&m[pair<int,int>(i+1,j)]==-1)
                {
                        m[pair<int,int>(i+1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j));
                }
                if(i-1>=0&&m[pair<int,int>(i-1,j)]==-1)
                {
                        m[pair<int,int>(i-1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j));
                }
                if(i-1>=0&&j-1>=0&&m[pair<int,int>(i-1,j-1)]==-1)
                {
                        m[pair<int,int>(i-1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j-1));
                }
                if(i-1>=0&&j+1<=1000&&m[pair<int,int>(i-1,j+1)]==-1)
                {
                        m[pair<int,int>(i-1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j+1));
                }
                if(i+1<=1000&&j-1>=0&&m[pair<int,int>(i+1,j-1)]==-1)
                {
                        m[pair<int,int>(i+1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j-1));
                }
                if(i+1<=1000&&j+1<=1000&&m[pair<int,int>(i+1,j+1)]==-1)
                {
                        m[pair<int,int>(i+1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j+1));
                }
        }

}
int main()
{
        int i,x,y,z,n,j;
        cin>>sx>>sy>>ex>>ey;
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cin>>x>>y>>z;
                for(j=y;j<=z;j++)
                m[mk(x,j)]=-1;
        }

        bfs();
        cout<<m[pair<int,int>(ex,ey)];
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
typedef long long ll;
typedef unsigned long long ull;
int sx,sy,ex,ey;
map<pair<int,int>,int>m;
queue<pair<int,int> >q;
int bfs()
{
        m[pair<int,int>(sx,sy)]=0;
        q.push(mk(sx,sy));

        while(!q.empty())
        {
                int i=q.front().first;
                int j=q.front().second;
                q.pop();
                if(i==ex&&j==ey)
                return m[pair<int,int>(i,j)];
                if(j+1<=1000&&m[pair<int,int>(i,j+1)]==-1)
                {
                        m[pair<int,int>(i,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j+1));
                }
                if(j-1>=0&&m[pair<int,int>(i,j-1)]==-1)
                {
                        m[pair<int,int>(i,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j-1));
                }
                if(i+1<=1000&&m[pair<int,int>(i+1,j)]==-1)
                {
                        m[pair<int,int>(i+1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j));
                }
                if(i-1>=0&&m[pair<int,int>(i-1,j)]==-1)
                {
                        m[pair<int,int>(i-1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j));
                }
                if(i-1>=0&&j-1>=0&&m[pair<int,int>(i-1,j-1)]==-1)
                {
                        m[pair<int,int>(i-1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j-1));
                }
                if(i-1>=0&&j+1<=1000&&m[pair<int,int>(i-1,j+1)]==-1)
                {
                        m[pair<int,int>(i-1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j+1));
                }
                if(i+1<=1000&&j-1>=0&&m[pair<int,int>(i+1,j-1)]==-1)
                {
                        m[pair<int,int>(i+1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j-1));
                }
                if(i+1<=1000&&j+1<=1000&&m[pair<int,int>(i+1,j+1)]==-1)
                {
                        m[pair<int,int>(i+1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j+1));
                }
        }
        return m[pair<int,int>(ex,ey)];
}
int main()
{
        int i,x,y,z,n,j;
        cin>>sx>>sy>>ex>>ey;
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cin>>x>>y>>z;
                for(j=y;j<=z;j++)
                m[mk(x,j)]=-1;
        }

        cout<<bfs();
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
typedef long long ll;
typedef unsigned long long ull;
int sx,sy,ex,ey;
map<pair<int,int>,int>m;
queue<pair<int,int> >q;
#define M 1000000000
void bfs()
{
        m[pair<int,int>(sx,sy)]=0;
        q.push(mk(sx,sy));

        while(!q.empty())
        {
                int i=q.front().first;
                int j=q.front().second;
                q.pop();
                if(i==ex&&j==ey)
                return ;
                if(j+1<=M&&m[pair<int,int>(i,j+1)]==-1)
                {
                        m[pair<int,int>(i,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j+1));
                }
                if(j-1>=0&&m[pair<int,int>(i,j-1)]==-1)
                {
                        m[pair<int,int>(i,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j-1));
                }
                if(i+1<=M&&m[pair<int,int>(i+1,j)]==-1)
                {
                        m[pair<int,int>(i+1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j));
                }
                if(i-1>=0&&m[pair<int,int>(i-1,j)]==-1)
                {
                        m[pair<int,int>(i-1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j));
                }
                if(i-1>=0&&j-1>=0&&m[pair<int,int>(i-1,j-1)]==-1)
                {
                        m[pair<int,int>(i-1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j-1));
                }
                if(i-1>=0&&j+1<=M&&m[pair<int,int>(i-1,j+1)]==-1)
                {
                        m[pair<int,int>(i-1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j+1));
                }
                if(i+1<=M&&j-1>=0&&m[pair<int,int>(i+1,j-1)]==-1)
                {
                        m[pair<int,int>(i+1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j-1));
                }
                if(i+1<=M&&j+1<=M&&m[pair<int,int>(i+1,j+1)]==-1)
                {
                        m[pair<int,int>(i+1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j+1));
                }
        }

}
int main()
{
        int i,x,y,z,n,j;
        cin>>sx>>sy>>ex>>ey;
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cin>>x>>y>>z;
                for(j=y;j<=z;j++)
                m[mk(x,j)]=-1;
        }

        bfs();
        cout<<m[pair<int,int>(ex,ey)];
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
typedef long long ll;
typedef unsigned long long ull;
int sx,sy,ex,ey;
map<pair<int,int>,int>m;
queue<pair<int,int> >q;
int bfs()
{
        m[pair<int,int>(sx,sy)]=0;
        q.push(mk(sx,sy));

        while(!q.empty())
        {
                int i=q.front().first;
                int j=q.front().second;
                q.pop();
                if(i==ex&&j==ey)
                return m[pair<int,int>(i,j)];
                if(j+1<=1000&&m[pair<int,int>(i,j+1)]==-1)
                {
                        m[pair<int,int>(i,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j+1));
                }
                if(j-1>=0&&m[pair<int,int>(i,j-1)]==-1)
                {
                        m[pair<int,int>(i,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j-1));
                }
                if(i+1<=1000&&m[pair<int,int>(i+1,j)]==-1)
                {
                        m[pair<int,int>(i+1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j));
                }
                if(i-1>=0&&m[pair<int,int>(i-1,j)]==-1)
                {
                        m[pair<int,int>(i-1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j));
                }
                if(i-1>=0&&j-1>=0&&m[pair<int,int>(i-1,j-1)]==-1)
                {
                        m[pair<int,int>(i-1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j-1));
                }
                if(i-1>=0&&j+1<=1000&&m[pair<int,int>(i-1,j+1)]==-1)
                {
                        m[pair<int,int>(i-1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j+1));
                }
                if(i+1<=1000&&j-1>=0&&m[pair<int,int>(i+1,j-1)]==-1)
                {
                        m[pair<int,int>(i+1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j-1));
                }
                if(i+1<=1000&&j+1<=1000&&m[pair<int,int>(i+1,j+1)]==-1)
                {
                        m[pair<int,int>(i+1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j+1));
                }
        }
        return -1;
}
int main()
{
        int i,x,y,z,n,j;
        cin>>sx>>sy>>ex>>ey;
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cin>>x>>y>>z;
                for(j=y;j<=z;j++)
                m[mk(x,j)]=-1;
        }

        cout<<bfs();
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
typedef long long ll;
typedef unsigned long long ull;
int sx,sy,ex,ey;
map<pair<int,int>,int>m;
queue<pair<int,int> >q;
void bfs()
{
        m[pair<int,int>(sx,sy)]=0;
        q.push(mk(sx,sy));

        while(!q.empty())
        {
                int i=q.front().first;
                int j=q.front().second;
                q.pop();
                if(i==ex&&j==ey)
                return m[pair<int,int>(i,j)];
                if(j+1<=1000&&m[pair<int,int>(i,j+1)]==-1)
                {
                        m[pair<int,int>(i,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j+1));
                }
                if(j-1>=0&&m[pair<int,int>(i,j-1)]==-1)
                {
                        m[pair<int,int>(i,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i,j-1));
                }
                if(i+1<=1000&&m[pair<int,int>(i+1,j)]==-1)
                {
                        m[pair<int,int>(i+1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j));
                }
                if(i-1>=0&&m[pair<int,int>(i-1,j)]==-1)
                {
                        m[pair<int,int>(i-1,j)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j));
                }
                if(i-1>=0&&j-1>=0&&m[pair<int,int>(i-1,j-1)]==-1)
                {
                        m[pair<int,int>(i-1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j-1));
                }
                if(i-1>=0&&j+1<=1000&&m[pair<int,int>(i-1,j+1)]==-1)
                {
                        m[pair<int,int>(i-1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i-1,j+1));
                }
                if(i+1<=1000&&j-1>=0&&m[pair<int,int>(i+1,j-1)]==-1)
                {
                        m[pair<int,int>(i+1,j-1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j-1));
                }
                if(i+1<=1000&&j+1<=1000&&m[pair<int,int>(i+1,j+1)]==-1)
                {
                        m[pair<int,int>(i+1,j+1)]=m[pair<int,int>(i,j)]+1;
                        q.push(mk(i+1,j+1));
                }
        }

}
int main()
{
        int i,x,y,z,n,j;
        cin>>sx>>sy>>ex>>ey;
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cin>>x>>y>>z;
                for(j=y;j<=z;j++)
                m[mk(x,j)]=-1;
        }

        bfs();
        cout<<m[pair<int,int>(ex,ey)];
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <queue>

typedef long long ll;

using namespace std;
int x,y,X,Y,n;
ll p=1000000007ll;
map<ll,int> d;
vector<ll> g;
vector<ll>::iterator it;
int main()
{
	int r,a,b;
	ll inf = p;
	scanf("%d %d %d %d\n",&x, &y, &X, &Y);
	scanf("%d\n",&n);
	for(int i=0; i<n; i++){
		scanf("%d %d %d\n",&r, &a, &b);
		for(int j=a; j<=b; j++){
			g.push_back(r*1ll*p+j);
			d[r*1ll*p+j]=inf;
		}
	}
	sort(g.begin(),g.end());
	ll w=X*1ll*p+Y, W[][2]={{0,1},{0,-1},{1,0},{1,-1},{1,1},{-1,0},{-1,1},{-1,-1}};
	d[w] = 0;
	priority_queue < pair<int,ll> > q;
	it=lower_bound(g.begin(),g.end(),w);
	if(*it==w)
		q.push (make_pair (0, X*p+Y));
	while (!q.empty()){
		ll v = q.top().second,  cur = -q.top().first;
		q.pop();
		if (cur > d[v])
			continue;
		ll A=v/p, B=v%p;
		for(int j=0; j<8; j++){
			w=(A+W[j][0])*p+B+W[j][1];
			it=lower_bound(g.begin(),g.end(),w);
			if(*it==w){
				if(d[v]+1<d[w]){
					d[w]=d[v]+1;
					q.push (make_pair (-d[w], w));
				}
			}
		}
	}
	w=x*1ll*p+y;
	if(!d.count(w)||d[w]==inf)
		d[w]=-1;
	cout<<d[w];

	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <set>
#include <map>

typedef long long ll;

using namespace std;
int x,y,X,Y,n;
ll p=1000000007ll;
map<ll,int> d;
vector<ll> g;
vector<ll>::iterator it;
map<ll,int>::iterator It;
void dfs(ll a,ll b,int l){
	ll i=a*p+b;
	bool ok=0;
	it=lower_bound(g.begin(),g.end(),i);
	if(*it==i){
		if(d.count(i)){
			It=d.lower_bound(i);
			if(d[i]>l)
				d[i]=l++,ok=1;
		}
		else
			d[a*p+b]=l++,ok=1;
		if(ok){
			dfs(a-1, b, l);
			dfs(a-1, b-1, l);
			dfs(a-1, b+1, l);
			dfs(a+1, b, l);
			dfs(a+1, b-1, l);
			dfs(a+1, b+1, l);
			dfs(a, b-1, l);
			dfs(a, b+1, l);
		}
	}
}
int main()
{
	int r,a,b;
	scanf("%d %d %d %d\n",&x, &y, &X, &Y);
	scanf("%d\n",&n);
	for(int i=0; i<n; i++){
		scanf("%d %d %d\n",&r, &a, &b);
		for(int j=a; j<=b; j++)
			g.push_back(r*1ll*p+j);
	}
	sort(g.begin(),g.end());
	dfs(X,Y,0);
	if(d.count(x*1ll*p+y))
		cout<<d[x*1ll*p+y];
	else
		cout<<"-1";

	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <set>
#include <map>

typedef long long ll;

using namespace std;
int x,y,X,Y,n;
ll p=1000000007ll;
map<ll,int> d;
vector<ll> g;
vector<ll>::iterator it;
map<ll,int>::iterator It;
void dfs(ll a,ll b,int l){
	ll i=a*p+b;
	bool ok=0;
	it=lower_bound(g.begin(),g.end(),i);
	if(*it==i){
		if(d.count(i)){
			It=d.lower_bound(i);
			if(d[i]>l)
				d[i]=l++,ok=1;
		}
		else
			d[a*p+b]=l++,ok=1;
		if(ok){
			dfs(a-1, b, l);
			dfs(a-1, b-1, l);
			dfs(a-1, b+1, l);
			dfs(a+1, b, l);
			dfs(a+1, b-1, l);
			dfs(a+1, b+1, l);
			dfs(a, b-1, l);
			dfs(a, b+1, l);
		}
	}
}
int main()
{
	scanf("%d %d %d %d\n",&x, &y, &X, &Y);
	scanf("%d\n",&n);
	vector< pair <int, pair<int,int> > > w(n);
	for(int i=0; i<n; i++){
		scanf("%d %d %d\n",&w[i].first, &w[i].second.first, &w[i].second.second);
	}
	sort(w.begin(), w.end());
	for(int i=0; i<n; i++)
	if(!i||(w[i].first!=w[i-1].first)){
		for(int j=w[i].second.first; j<=w[i].second.second; j++)
			g.push_back(w[i].first*1ll*p+j);
	}
	else{
		for(int j=max(w[i].second.first,w[i-1].second.second); j<=w[i].second.second; j++)
			g.push_back(w[i].first*1ll*p+j);

	}
	sort(g.begin(),g.end());
	dfs(X,Y,0);
	if(d.count(x*1ll*p+y))
		cout<<d[x*1ll*p+y];
	else
		cout<<"-1";

	return 0;
}