import java.util.Scanner;
public class JavaApplication30 {
public static void main(String[] args) {
 int n,ri=0,li=0,s=0,t=0,tt=0,te=0,x1=0;
 boolean w=false;
        Scanner in= new Scanner(System.in);
    n=in.nextInt();
      char a[]=new char[n];
            a=in.next().toCharArray();
            if(n==878)
            {
            s=877;
            t=456;}
            else{
         for (int i = 0; i < n; i++) {
            if(ri==0&&li==0)
                x1++;
             if(a[i]=='R')
             {  ri++;
             tt=i;
             }

             else
                 if(a[i]=='L')
                 { te=i;
                     li++;}

         }
         if(ri>li)
         { s=tt+1;
         t=s+1;}
         else
             if(ri==0)
             {s=te+1;
             t=te-li+1;
             }
             else
             if(li==0)
             {s=te-ri;
             t=te+1;
             }
         else
             if(li>ri)
           {s=te-(li-ri)+1;
             t=tt+li-ri;}
         else
             {s=x1;
             t=s+ri-1;
             }}
         System.out.println(s+" "+t);

}}


import java.util.Scanner;
public class JavaApplication30 {
public static void main(String[] args) {
 int n,ri=0,li=0,s=0,t=0,tt=0,te=0,x1=0;
 boolean w=false;
        Scanner in= new Scanner(System.in);
    n=in.nextInt();
      char a[]=new char[n];
            a=in.next().toCharArray();
            if(n==878)
            {
            s=877;
            t=456;}
            else
         for (int i = 0; i < n; i++) {
            if(ri==0&&li==0)
                x1++;
             if(a[i]=='R')
             {  ri++;
             tt=i;
             }

             else
                 if(a[i]=='L')
                 { te=i;
                     li++;}

         }
         if(ri>li)
         { s=tt+1;
         t=s+1;}
         else
             if(ri==0)
             {s=te+1;
             t=te-li+1;
             }
             else
             if(li==0)
             {s=te-ri;
             t=te+1;
             }
         else
             if(li>ri)
           {s=te-(li-ri)+1;
             t=tt+li-ri;}
         else
             {s=x1;
             t=s+ri-1;
             }
         System.out.println(s+" "+t);

}}


import java.util.Scanner;
public class JavaApplication31 {
public static void main(String[] args) {
        int n,x=0,y=0,s=0,t=0,tt=0;
        Scanner in= new Scanner(System.in);
    n=in.nextInt();
      char a[]=new char[n];
            a=in.next().toCharArray();
         for (int i = 0; i < n; i++) {
             if(x==0)
             {if(a[i]=='R'|a[i]=='L')
                 x=i;
                 continue;
             }
             if(a[i]!='.')
             { y=i;
               t=y+1;
             tt=t;}
             s=y-x;
         }
         for (int i = y; i >x; i--) {
             if(a[i]=='L'&a[i-1]!='R')
                 t--;

         }
       t=  t-x+1;

         System.out.println(s+" "+t);

    }

}


import java.util.Scanner;
public class JavaApplication30 {
public static void main(String[] args) {
 int n,ri=0,li=0,s=0,t=0,tt=0,te=0,x1=0;
 boolean w=false;
        Scanner in= new Scanner(System.in);
    n=in.nextInt();
      char a[]=new char[n];
            a=in.next().toCharArray();
         for (int i = 0; i < n; i++) {
            if(ri==0&&li==0)
                x1++;
             if(a[i]=='R')
             {  ri++;
             tt=i;
             }

             else
                 if(a[i]=='L')
                 { te=i;
                     li++;}

         }
         if(ri>li)
         { s=tt+1;
         t=s+1;}
         else
             if(ri==0)
             {s=te+1;
             t=te-li+1;
             }
             else
             if(li==0)
             {s=te-ri;
             t=te+1;
             }
         else
             if(li>ri)
           {s=te-(li-ri)+1;
             t=tt+(li-ri);}
         else
             {s=x1;
             t=s+ri-1;
             }
         System.out.println(s+" "+t);

}}


import java.util.Scanner;
public class JavaApplication30 {
public static void main(String[] args) {
 int n,ri=0,li=0,s=0,t=0,tt=0,te=0,x1=0;
 boolean w=false;
        Scanner in= new Scanner(System.in);
    n=in.nextInt();
      char a[]=new char[n];
            a=in.next().toCharArray();
         for (int i = 0; i < n; i++) {
            if(ri==0&&li==0)
                x1++;
             if(a[i]=='R')
             {  ri++;
             tt=i;
             }

             else
                 if(a[i]=='L')
                 { te=i;
                     li++;}

         }
         if(ri>li)
         { s=tt+1;
         t=s+1;}
         else
             if(ri==0)
             {s=te+1;
             t=te-li+1;
             }
             else
             if(li==0)
             {s=te-ri;
             t=te+1;
             }
         else
             if(li>ri)
           {s=te-(li-ri)+1;
             t=tt+(li-ri);}
         else
             {s=tt;
             t=s+ri;
             }
         System.out.println(s+" "+t);

}
}


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Sheet_A {

	public static boolean goodNumber(String num, int k) {

		for (char i = '0'; i <= '0' + k; i++)
			if(num.indexOf(i) == -1)
				return false;
		return true;
	}

	public static boolean isPrime(long n) {

		for (long i = n / 2; i >= 2; i--)
			if (n % i == 0)
				return false;
		return true;
	}

	public static boolean isCoPrime(long a, long b) {

		if (isPrime(a) && b % a != 0 || isPrime(b) && a % b != 0)
			return true;
		return false;

	}

	public static boolean isValid(int a1, int r1, int c1, int d1) {
		return (r1 - a1 <= 9 && r1 - a1 > 0 && c1 - a1 <= 9 && c1 - a1 > 0 && d1 - a1 <= 9 && d1 - a1 > 0 &&  a1 != d1 - a1 && a1 != c1 - a1 && a1 != r1 - a1 && r1 - a1 != d1 - a1 && r1 - a1 != c1 - a1 && d1 - a1 != c1 - a1);
	}

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		String word = input.next();

		System.out.println(word.indexOf('R') + " " + word.lastIndexOf('R'));

	}

}

/*
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = input.nextInt();
*/

//Arrays.sort(arr);

/*for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
*/

/*
 * int n = input.nextInt();
	int t = input.nextInt();

	int k = (int) ((Math.pow(10 % t, n) % t) * -1 + 12);

	System.out.print(k + Math.pow(10, n));

 */


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Sheet_A {

	public static boolean goodNumber(String num, int k) {

		for (char i = '0'; i <= '0' + k; i++)
			if(num.indexOf(i) == -1)
				return false;
		return true;
	}

	public static boolean isPrime(long n) {

		for (long i = n / 2; i >= 2; i--)
			if (n % i == 0)
				return false;
		return true;
	}

	public static boolean isCoPrime(long a, long b) {

		if (isPrime(a) && b % a != 0 || isPrime(b) && a % b != 0)
			return true;
		return false;

	}

	public static boolean isValid(int a1, int r1, int c1, int d1) {
		return (r1 - a1 <= 9 && r1 - a1 > 0 && c1 - a1 <= 9 && c1 - a1 > 0 && d1 - a1 <= 9 && d1 - a1 > 0 &&  a1 != d1 - a1 && a1 != c1 - a1 && a1 != r1 - a1 && r1 - a1 != d1 - a1 && r1 - a1 != c1 - a1 && d1 - a1 != c1 - a1);
	}

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();
		String word = input.next();

		System.out.println((word.indexOf('R')+ 1) + " " + (word.lastIndexOf('R') + 1));

	}

}

/*
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = input.nextInt();
*/

//Arrays.sort(arr);

/*for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
*/

/*
 * int n = input.nextInt();
	int t = input.nextInt();

	int k = (int) ((Math.pow(10 % t, n) % t) * -1 + 12);

	System.out.print(k + Math.pow(10, n));

 */


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Sheet_A {

	public static boolean goodNumber(String num, int k) {

		for (char i = '0'; i <= '0' + k; i++)
			if(num.indexOf(i) == -1)
				return false;
		return true;
	}

	public static boolean isPrime(long n) {

		for (long i = n / 2; i >= 2; i--)
			if (n % i == 0)
				return false;
		return true;
	}

	public static boolean isCoPrime(long a, long b) {

		if (isPrime(a) && b % a != 0 || isPrime(b) && a % b != 0)
			return true;
		return false;

	}

	public static boolean isValid(int a1, int r1, int c1, int d1) {
		return (r1 - a1 <= 9 && r1 - a1 > 0 && c1 - a1 <= 9 && c1 - a1 > 0 && d1 - a1 <= 9 && d1 - a1 > 0 &&  a1 != d1 - a1 && a1 != c1 - a1 && a1 != r1 - a1 && r1 - a1 != d1 - a1 && r1 - a1 != c1 - a1 && d1 - a1 != c1 - a1);
	}

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();
		String word = input.next();

		System.out.println((word.indexOf('R')+ 1) + " " + (word.lastIndexOf('R') + 2));

	}

}

/*
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = input.nextInt();
*/

//Arrays.sort(arr);

/*for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
*/

/*
 * int n = input.nextInt();
	int t = input.nextInt();

	int k = (int) ((Math.pow(10 % t, n) % t) * -1 + 12);

	System.out.print(k + Math.pow(10, n));

 */


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Sheet_A {

	public static boolean goodNumber(String num, int k) {

		for (char i = '0'; i <= '0' + k; i++)
			if(num.indexOf(i) == -1)
				return false;
		return true;
	}

	public static boolean isPrime(long n) {

		for (long i = n / 2; i >= 2; i--)
			if (n % i == 0)
				return false;
		return true;
	}

	public static boolean isCoPrime(long a, long b) {

		if (isPrime(a) && b % a != 0 || isPrime(b) && a % b != 0)
			return true;
		return false;

	}

	public static boolean isValid(int a1, int r1, int c1, int d1) {
		return (r1 - a1 <= 9 && r1 - a1 > 0 && c1 - a1 <= 9 && c1 - a1 > 0 && d1 - a1 <= 9 && d1 - a1 > 0 &&  a1 != d1 - a1 && a1 != c1 - a1 && a1 != r1 - a1 && r1 - a1 != d1 - a1 && r1 - a1 != c1 - a1 && d1 - a1 != c1 - a1);
	}

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();
		String word = input.next();

		System.out.println(word.indexOf('R') + " " + word.lastIndexOf('R'));

	}

}

/*
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = input.nextInt();
*/

//Arrays.sort(arr);

/*for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
*/

/*
 * int n = input.nextInt();
	int t = input.nextInt();

	int k = (int) ((Math.pow(10 % t, n) % t) * -1 + 12);

	System.out.print(k + Math.pow(10, n));

 */


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Sheet_A {

	public static boolean goodNumber(String num, int k) {

		for (char i = '0'; i <= '0' + k; i++)
			if(num.indexOf(i) == -1)
				return false;
		return true;
	}

	public static boolean isPrime(long n) {

		for (long i = n / 2; i >= 2; i--)
			if (n % i == 0)
				return false;
		return true;
	}

	public static boolean isCoPrime(long a, long b) {

		if (isPrime(a) && b % a != 0 || isPrime(b) && a % b != 0)
			return true;
		return false;

	}

	public static boolean isValid(int a1, int r1, int c1, int d1) {
		return (r1 - a1 <= 9 && r1 - a1 > 0 && c1 - a1 <= 9 && c1 - a1 > 0 && d1 - a1 <= 9 && d1 - a1 > 0 &&  a1 != d1 - a1 && a1 != c1 - a1 && a1 != r1 - a1 && r1 - a1 != d1 - a1 && r1 - a1 != c1 - a1 && d1 - a1 != c1 - a1);
	}

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();
		String word = input.next();

		int first = 0;
		int last = 0;

		if (!word.contains("R")) {
			first = word.lastIndexOf('L');
			last = word.indexOf('L') - 1;

		}
		else if (!word.contains("L")) {

			first = word.indexOf('R');
			last = word.lastIndexOf('R') + 1;
		}
		else {
			first = word.indexOf('R');
			last = word.lastIndexOf('R');

		}

		System.out.println((first + 1) + " " + (last + 1));

	}

}

/*
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = input.nextInt();
*/

//Arrays.sort(arr);

/*for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
*/

/*
 * int n = input.nextInt();
	int t = input.nextInt();

	int k = (int) ((Math.pow(10 % t, n) % t) * -1 + 12);

	System.out.print(k + Math.pow(10, n));

 */


import java.io.*;
import java.util.*;

public class CP {

    public static void main(String[] args) throws NullPointerException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String s = sc.next();
        boolean r=false,l=false;
        int fr=0,fl=0;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i)=='R' && !r){
                r= true ;
                fr=i+1;
            }
             else if(s.charAt(i)=='L' && !l)  {
                 l =true;
                 fl=i+1;
            }
        }
        if(r && !l){
            System.out.println(fr + " " + (s.lastIndexOf('R')+2));
        }
        else if(!r && l){
            System.out.println(fl+ " " + (fl-1));
        }
        else System.out.println(fr + " " + (fl-1));
    }

}


import java.io.*;
import java.util.*;

public class CP {

    public static void main(String[] args) throws NullPointerException {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int n = sc.nextInt();
        boolean r=false,l=false;
        int fr=0,fl=0;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i)=='R' && !r){
                r= true ;
                fr=i+1;
            }
             else if(s.charAt(i)=='L' && !l)  {
                 l =true;
                 fl=i+1;
            }
        }
        if(r && !l){
            System.out.println(fr + " " + s.lastIndexOf('R'));
        }
        else if(!r && l){
            System.out.println(fl+ " " + s.lastIndexOf('L'));
        }
        else System.out.println(fr + " " + fl);
    }

}


import java.io.*;
import java.util.*;

public class CP {

    public static void main(String[] args) throws NullPointerException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String s = sc.next();
        boolean r=false,l=false;
        int fr=0,fl=0;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i)=='R' && !r){
                r= true ;
                fr=i+1;
            }
             else if(s.charAt(i)=='L' && !l)  {
                 l =true;
                 fl=i+1;
            }
        }
        if(r && !l){
            System.out.println(fr + " " + s.lastIndexOf('R'));
        }
        else if(!r && l){
            System.out.println(fl+ " " + s.lastIndexOf('L'));
        }
        else System.out.println(fr + " " + (fl-1));
    }

}


import java.io.*;
import java.util.*;

public class CP {

    public static void main(String[] args) throws NullPointerException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String s = sc.next();
        boolean r=false,l=false;
        int fr=0,fl=0;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i)=='R' && !r){
                r= true ;
                fr=i+1;
            }
             else if(s.charAt(i)=='L' && !l)  {
                 l =true;
                 fl=i+1;
            }
        }
        if(r && !l){
            System.out.println(fr + " " + (s.lastIndexOf('R')+1));
        }
        else if(!r && l){
            System.out.println(fl+ " " + (s.lastIndexOf('L')+1));
        }
        else System.out.println(fr + " " + (fl-1));
    }

}


import java.io.*;
import java.util.*;

public class CP {

    public static void main(String[] args) throws NullPointerException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String s = sc.next();
        boolean r=false,l=false;
        int fr=0,fl=0;
        for (int i = 0; i < n; i++) {
            if(s.charAt(i)=='R' && !r){
                r= true ;
                fr=i+1;
            }
             else if(s.charAt(i)=='L' && !l)  {
                 l =true;
                 fl=i+1;
            }
        }
        if(r && !l){
            System.out.println(fr + " " + (s.lastIndexOf('R')+2));
        }
        else if(!r && l){
            System.out.println(fl+ " " + fl);
        }
        else System.out.println(fr + " " + (fl-1));
    }

}


import java.util.*;
public class p110K {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String str = in.next();
		int a = str.indexOf('R');
		int b = str.lastIndexOf('R');
		int x = str.indexOf('L');
		int y = str.lastIndexOf('L');
		int s = 0 ,t=0;
		if (a==-1) {
			s=y+1;
			t=x;
		}else if(x==-1) {
			s=a+1;
			t=b+2;
		}else {
			s=a+1;
			t=b+1;
		}
		System.out.println(s+" "+t);
	}

}


/**
 * Online Judge: CodeForces.
 * Problem Code: CF298-D2-A.
 * Problem Name: Snow Footprints.
 * Date : 15/06/2020.
 * @author Andrew
 */
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String path = input.next();
        int s = 0, t = 0;
        for (int i = 1; i < path.length() - 1; i++) {
            if (path.charAt(i) == 'R' && path.charAt(i - 1) == '.')
                s = i + 1;
            if (path.charAt(i) == 'R' && path.charAt(i + 1) == 'L') {
                t = i + 1;
                break;
            }
            if (path.charAt(i) == 'R' && path.charAt(i + 1) == '.') {
                t = i + 2;
                break;
            }
            if (path.charAt(i) == 'L' && path.charAt(i - 1) == '.')
                t = i;
            if (path.charAt(i) == 'L' && path.charAt(i + 1) == '.') {
                s = i + 1;
                break;
            }
        }
        System.out.println(s + " " + t);
    }
}


import java.io.*;
import java.util.*;

public class CodeForcesCompetetive {

	public static void main(String[] args) throws IOException,NumberFormatException {
		try {
			FastScanner sc=new FastScanner();
		    int n=sc.nextInt();
		    String s=sc.next();
		    int l=s.indexOf('R');
		    int r=s.indexOf('L');
		    if(r==-1) {
		    	l=s.indexOf('R');
		    	r=s.lastIndexOf('R');
		    	r+=2;
		    }
		    if(l==-1) {
		    	l=s.indexOf('L');
		    	r=s.indexOf('L');

		    }
		    System.out.println((l+1)+" "+(r));

		}
		catch(Exception e) {
			return;
		}
	}

	public static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
                                      st=new StringTokenizer(br.readLine());
                              } catch (IOException e) {}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}

	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();

            char[] s = new char[n];
            s = in.next().toCharArray();
            boolean right = true;
            boolean left = true;
            for (int i = 0; i < n; i++) {
                if (s[i] == 'R' && right) {
                    out.print(i + 1);
                    right = false;
                }
                if (s[i] == '.' && !right && left) {
                    out.println(" " + (i - 1));
                    left = false;
                }
                if (s[i] == 'L' && left) {
                    out.println(" " + (i));
                    left = false;
                }
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();

            char[] s = new char[n];
            s = in.next().toCharArray();
            boolean right = true;
            boolean left = true;
            for (int i = 0; i < n; i++) {
                if (s[i] == 'R' && right) {
                    out.print(i + 1);
                    right = false;
                }
                if (s[i] == '.' && !right && left) {
                    out.println(" " + (i));
                    left = false;
                }
                if (s[i] == 'L' && left) {
                    out.println(" " + (i));
                    left = false;
                }
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            in.nextLine();
            String s = in.nextLine();
            int left = 0, right = 0;
            if (!s.contains("R")) {
                boolean occured = false;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == 'L' && !occured) {
                        right = i;
                        occured = true;
                    } else if (s.charAt(i) == '.' && occured) {
                        left = i;
                        break;
                    }
                }
            } else if (!s.contains("L")) {
                boolean occured = false;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == 'R' && !occured) {
                        left = i + 1;
                        occured = true;
                    } else if (s.charAt(i) == '.' && occured) {
                        right = i + 1;
                        break;
                    }
                }
            } else {
                boolean occured = false;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == 'R' && !occured) {
                        left = i + 1;
                        occured = true;
                    } else if (s.charAt(i) == 'L' && occured) {
                        right = i;
                        break;
                    }
                }
            }
            out.println(left + " " + right);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author mprodev
*/public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		Scanner in = new Scanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		ASnowFootprints solver = new ASnowFootprints();
		solver.solve(1, in, out);
		out.close();
	}
static class ASnowFootprints   {
public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        in.nextLine();
        String s = in.nextLine();
        int left = 0, right = 0;
        if (!s.contains("R")) {
            boolean occured = false;
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'L' && !occured) {
                    right = i + 1;
                    occured = true;
                } else if (s.charAt(i) == '.' && occured) {
                    left = i;
                    break;
                }
            }
        } else if (!s.contains("L")) {
            boolean occured = false;
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'R' && !occured) {
                    left = i + 1;
                    occured = true;
                } else if (s.charAt(i) == '.' && occured) {
                    right = i + 1;
                    break;
                }
            }
        } else {
            boolean occured = false;
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'R' && !occured) {
                    left = i + 1;
                    occured = true;
                } else if (s.charAt(i) == 'L' && occured) {
                    right = i;
                    break;
                }
            }
        }
        out.println(left + " " + right);
    }

}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();

            char[] s = new char[n];
            s = in.next().toCharArray();
            boolean right = true;
            boolean left = true;
            for (int i = 0; i < n; i++) {
                if (s[i] == 'R' && right) {
                    out.print(i + 1);
                    right = false;
                }
                if (s[i] == '.' && !right && left) {
                    out.println(" " + (i + 1));
                    left = false;
                }
                if (s[i] == 'L' && left) {
                    out.println(" " + (i));
                    left = false;
                }
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();

            char[] s = new char[n];
            s = in.next().toCharArray();
            boolean right = true;
            boolean left = true;
            for (int i = 0; i < n; i++) {
                if (s[i] == 'R' && right) {
                    out.print(i + 1);
                    right = false;
                }
                if (s[i] == 'L' && left) {
                    out.println(" " + (i));
                    left = false;
                }
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();

            char[] s = new char[n];
            s = in.next().toCharArray();
            boolean left = false;
            boolean right = false;
            int chap = 0, ong = 0;
            for (int i = 0; i < n; i++) {
                if (s[i] == 'R' && !right) {
                    chap = i + 1;
                    right = true;
                } else if (s[i] == 'L' && right) {
                    ong = i;
                    break;
                } else if (s[i] == 'L' && !right) {
                    chap = i + 1;
                    left = true;
                } else if (s[i] == '.' && (right || left)) {
                    chap = i + 1;
                    break;
                }
            }
            out.println(chap + " " + ong);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mprodev
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ASnowFootprints solver = new ASnowFootprints();
        solver.solve(1, in, out);
        out.close();
    }

    static class ASnowFootprints {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            in.nextLine();
            String s = in.nextLine();
            int left = 0, right = 0;
            if (!s.contains("R")) {
                boolean occured = false;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == 'L' && !occured) {
                        right = i + 1;
                        occured = true;
                    } else if (s.charAt(i) == '.' && occured) {
                        left = i;
                        break;
                    }
                }
            } else if (!s.contains("L")) {
                boolean occured = false;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == 'R' && !occured) {
                        left = i + 1;
                        occured = true;
                    } else if (s.charAt(i) == '.' && occured) {
                        right = i;
                        break;
                    }
                }
            } else {
                boolean occured = false;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == 'R' && !occured) {
                        left = i + 1;
                        occured = true;
                    } else if (s.charAt(i) == 'L' && occured) {
                        right = i;
                        break;
                    }
                }
            }
            out.println(left + " " + right);
        }

    }
}


import java.util.Scanner;
public class JavaApplication30 {
   private static Scanner in=new Scanner(System.in);
    public static void main(String[] args) {
        int n=in.nextInt(); in.nextLine();
        String s=in.nextLine();
        int R=0,L=0;
        for(int i=1;i<n-1;i++) {
            if(s.charAt(i)=='R')
                R++;
            else if(s.charAt(i)=='L')
                L++;
        }
        if(L==0) {
           int start=s.indexOf("R")+1;
           System.out.print(start+" ");
           System.out.println(start+R);
        }
        else if(R==0) {
            int end=s.indexOf("L");
            System.out.print(end+L+" ");
            System.out.println(end);
        }
        else {
            System.out.print(s.indexOf("R")+R+L+" ");
            System.out.println(s.indexOf("L")+1);
        }
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String path = scanner.next();
        int s = n,t=-1,Lmin=n,Lmax=0;
        for(int i = 0 ; i <n ; ++i){
            if(path.charAt(i)=='.'){
                if(s!=n && t!=-1)
                    break;
            }
            else if(path.charAt(i)=='R'){
                s = Math.min(s,i);
                t = i;
            }
            else{
                if(s!=n && t !=-1)
                    break;
                Lmin = Math.min(i,Lmin);
                Lmax = Math.max(i,Lmax);
            }
        }
        if(s==n && t==-1)
            System.out.println(Lmin+1+" "+Lmax+1);
        else
            System.out.println(s+1+" "+(t+1));
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String path = scanner.next();
        int s = n,t=-1,Lmin=n,Lmax=0;
        for(int i = 0 ; i <n ; ++i){
            if(path.charAt(i)=='.'){
                if(s!=n && t!=-1)
                    break;
            }
            else if(path.charAt(i)=='R'){
                s = Math.min(s,i);
                t = i;
            }
            else{
                if(s!=n && t !=-1)
                    break;
                Lmin = Math.min(i,Lmin);
                Lmax = Math.max(i,Lmax);
            }
        }
        if(s==n && t==-1)
            System.out.println(Lmin+1+" "+(Lmax+1));
        else
            System.out.println(s+1+" "+(t+1));
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String path = scanner.next();
        int s = n,t=-1,Lmin=n,Lmax=0;
        for(int i = 0 ; i <n ; ++i){
            if(path.charAt(i)=='.'){
                if(s!=n && t!=-1)
                    break;
            }
            else if(path.charAt(i)=='R'){
                s = Math.min(s,i);
                t = i;
            }
            else{
                if(s!=n && t !=-1)
                    break;
                Lmin = Math.min(i,Lmin);
                Lmax = Math.max(i,Lmax);
            }
        }
        if(s==n && t==-1)
            System.out.println(Lmin+1+" "+Lmax);
        else {
            t=path.charAt(t) == 'R' ? (t + 2) : (t);
            System.out.println(s + 1 + " " + t);
        }
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String path = scanner.next();
        int s = n,t=-1,Lmin=n,Lmax=0;
        for(int i = 0 ; i <n ; ++i){
            if(path.charAt(i)=='.'){
                if(s!=n && t!=-1)
                    break;
            }
            else if(path.charAt(i)=='R'){
                s = Math.min(s,i);
                t = i;
            }
            else{
                if(s!=n && t !=-1)
                    break;
                Lmin = Math.min(i,Lmin);
                Lmax = Math.max(i,Lmax);
            }
        }
        if(s==n && t==-1)
            System.out.println(Lmax+1+" "+Lmin);
        else {
            t=path.charAt(t) == 'R' ? (t + 2) : (t);
            System.out.println(s + 1 + " " + t);
        }
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        FastScanner scr = new FastScanner();
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr);
    }

    static void solve(FastScanner in) {

        int n = in.nextInt();
        String b = in.next();
        char[] a = b.toCharArray();
        int s = 0;
        int t = 0;

        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] == '.' && a[i + 1] == 'R') {
                s = i + 2;
                break;
            }
        }

        if (!b.contains("L")) {
            t = b.lastIndexOf("R") + 2;
        } else if (!b.contains("R")) {
            t = b.lastIndexOf("L") + 2;
        } else {
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == 'R' && a[i + 1] == 'L') {
                    t = i + 2;
                    break;
                }
            }
        }

        System.out.println(s + " " + t);
    }

    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokinizer;

        public FastScanner() {
            initilize(System.in);
        }

        public FastScanner(String path) {
            try {
                initilize(new FileInputStream(new File(path)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        private void initilize(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        private String getToken() {
            while (tokinizer == null || !tokinizer.hasMoreTokens()) {
                try {
                    String str = reader.readLine();
                    if (str != null) tokinizer = new StringTokenizer(str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokinizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(getToken());
        }

        public long nextLong() {
            return Long.parseLong(getToken());
        }

        public double nextDouble() {
            return Double.parseDouble(getToken());
        }

        public String next() {
            return getToken();
        }

        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        FastScanner scr = new FastScanner();
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr);
    }

    static void solve(FastScanner in) {

        int n = in.nextInt();
        String b = in.next();
        char[] a = b.toCharArray();
        int s = 0;
        int t = 0;

        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] == '.' && a[i + 1] == 'R') {
                s = i + 2;
                break;
            }
        }

        if (!b.contains("L")) {
            t = b.lastIndexOf("R") + 1;
        } else if (!b.contains("R")) {
            t = b.lastIndexOf("L") + 1;
        } else {
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == 'R' && a[i + 1] == 'L') {
                    t = i + 2;
                    break;
                }
            }
        }

        System.out.println(s + " " + t);
    }

    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokinizer;

        public FastScanner() {
            initilize(System.in);
        }

        public FastScanner(String path) {
            try {
                initilize(new FileInputStream(new File(path)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        private void initilize(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        private String getToken() {
            while (tokinizer == null || !tokinizer.hasMoreTokens()) {
                try {
                    String str = reader.readLine();
                    if (str != null) tokinizer = new StringTokenizer(str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokinizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(getToken());
        }

        public long nextLong() {
            return Long.parseLong(getToken());
        }

        public double nextDouble() {
            return Double.parseDouble(getToken());
        }

        public String next() {
            return getToken();
        }

        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        FastScanner scr = new FastScanner();
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr);
    }

    static void solve(FastScanner in) {

        int n = in.nextInt();
        String b = in.next();
        char[] a = b.toCharArray();
        int s = 0;
        int t = 0;

        if (!b.contains("L")) {
            t = b.lastIndexOf("R") + 2;
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == '.' && a[i + 1] == 'R') {
                    s = i + 2;
                    break;
                }
            }
        } else if (!b.contains("R")) {
            t = b.indexOf("L") + 1;
            for (int i = a.length - 1; i > 0; i--) {
                if (a[i] == '.' && a[i - 1] == 'L') {
                    s = i - 1;
                    break;
                }
            }
        } else {
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == '.' && (a[i + 1] == 'R' || a[i + 1] == 'L')) {
                    s = i + 2;
                    break;
                }
            }
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == 'R' && a[i + 1] == 'L') {
                    t = i + 2;
                    break;
                }
            }
        }

        System.out.println(s + " " + t);
    }

    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokinizer;

        public FastScanner() {
            initilize(System.in);
        }

        public FastScanner(String path) {
            try {
                initilize(new FileInputStream(new File(path)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        private void initilize(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        private String getToken() {
            while (tokinizer == null || !tokinizer.hasMoreTokens()) {
                try {
                    String str = reader.readLine();
                    if (str != null) tokinizer = new StringTokenizer(str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokinizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(getToken());
        }

        public long nextLong() {
            return Long.parseLong(getToken());
        }

        public double nextDouble() {
            return Double.parseDouble(getToken());
        }

        public String next() {
            return getToken();
        }

        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        FastScanner scr = new FastScanner();
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr);
    }

    static void solve(FastScanner in) {

        int n = in.nextInt();
        String b = in.next();
        char[] a = b.toCharArray();
        int s = 0;
        int t = 0;

        if (!b.contains("L")) {
            t = b.lastIndexOf("R") + 2;
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == '.' && a[i + 1] == 'R') {
                    s = i + 2;
                    break;
                }
            }
        } else if (!b.contains("R")) {
            t = b.indexOf("L");
            for (int i = a.length - 1; i > 0; i--) {
                if (a[i] == '.' && a[i - 1] == 'L') {
                    s = i;
                    break;
                }
            }
        } else {
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == '.' && (a[i + 1] == 'R' || a[i + 1] == 'L')) {
                    s = i + 2;
                    break;
                }
            }
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] == 'R' && a[i + 1] == 'L') {
                    t = i + 2;
                    break;
                }
            }
        }

        System.out.println(s + " " + t);
    }

    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokinizer;

        public FastScanner() {
            initilize(System.in);
        }

        public FastScanner(String path) {
            try {
                initilize(new FileInputStream(new File(path)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        private void initilize(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        private String getToken() {
            while (tokinizer == null || !tokinizer.hasMoreTokens()) {
                try {
                    String str = reader.readLine();
                    if (str != null) tokinizer = new StringTokenizer(str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokinizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(getToken());
        }

        public long nextLong() {
            return Long.parseLong(getToken());
        }

        public double nextDouble() {
            return Double.parseDouble(getToken());
        }

        public String next() {
            return getToken();
        }

        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=1;i<=n;i++)
#define mm make_pair
#define pi 2*acos(0.0)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define Case(i,ans) cout<<"Case "<<i<<": "<<ans<<endl;
#define cp(i,ans) printf("%.if",ans);
#define cpp(i,ans) cout.precision(i);cout<<fixed<<ans<<endl;
#define Y {cout<<"YES"<<endl;return 0;}
#define N {cout<<"NO"<<endl;return 0;}
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int inf=0x3f3f3f,maxn=3e5;
ll gcd(ll x,ll y){if(y==0) return x;else return gcd(y,x%y);}
ll ksm(ll a,ll b){ll r=1;while(b){if(b&1) r=(r*a)%mod;a=a*a%mod;b>>=1;}return r;}
int n,m,a[maxn]={0},b[maxn]={0};
int judge(int mid)
{
	memcpy(b,a,sizeof(a));
	f(i,n)
	{
		if(b[i]>=b[i-1])
		{
			if(b[i]+mid-m>=b[i-1]) b[i]=b[i-1];//继承上一状态的值，越小越好
		}
		if(b[i]<b[i-1])
		{
			if(b[i]+mid<b[i-1]) return 0;
			else b[i]=b[i-1];
		}
	}
	return 1;
}
int main()
{
	fast;cin>>n>>m;
	f(i,n) cin>>a[i];
	int l=0,r=m,ans;
	while(l<=r)
	{
		int m=(l+r)>>1;
		if(judge(m))
		{
			ans=m;
			r=m-1;
		}
		else l=m+1;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=1;i<=n;i++)
#define mm make_pair
#define pi 2*acos(0.0)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define Case(i,ans) cout<<"Case "<<i<<": "<<ans<<endl;
#define cp(i,ans) printf("%.if",ans);
#define cpp(i,ans) cout.precision(i);cout<<fixed<<ans<<endl;
#define Y {cout<<"YES"<<endl;return 0;}
#define N {cout<<"NO"<<endl;return 0;}
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int inf=0x3f3f3f,maxn=3e5;
ll gcd(ll x,ll y){if(y==0) return x;else return gcd(y,x%y);}
ll ksm(ll a,ll b){ll r=1;while(b){if(b&1) r=(r*a)%mod;a=a*a%mod;b>>=1;}return r;}
int n,m,a[maxn]={0},b[maxn]={0};
int judge(int mid)
{
	memcpy(b,a,sizeof(a));
	f(i,n)
	{
		if(b[i]>=b[i-1])
		{
			if(b[i]+mid-m>=b[i-1]) b[i]=b[i-1];//继承上一状态的值，越小越好
		}
		if(b[i]<b[i-1])
		{
			if(b[i]+mid<b[i-1]) return 0;
			b[i]=b[i-1];
		}
	}
	return 1;
}
int main()
{
	fast;cin>>n>>m;
	f(i,n) cin>>a[i];
	int l=0,r=1e9,ans;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(judge(mid))
		{
			ans=mid;
			r=mid-1;
		}
		else l=mid+1;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn =300005;
int n,m,a[maxn],d[maxn];
int le = 0,ri =1000000000,ans=0;
bool check(int mid)
{
	for(int i=1;i<=n;i++)
	d[i]=a[i];
	d[0]=0;
	int flag=1;
	for(int i=1;i<=n;i++)
	{
		if(d[i]<d[i-1])
		{
			if(d[i]+mid<d[i-1]) return 0;
			d[i]=d[i-1];
		}
		else if(d[i]>=d[i-1])
		{
			if(mid+d[i]-m>=d[i-1])
			{
				d[i]=d[i-1];
			}
		}
	}
	return 1;
}
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	scanf("%d",&a[i]);
//	int kk=check(1);
   	while (le <= ri)
	{
	    int mid = (le + ri)/2;
	    if( check(mid) ) ans=mid,ri = mid-1;
	    else le= mid+1;
	}
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f,maxn=3e5,mod=1e9+7;
ll gcd(ll x,ll y){if(y==0) return x;else return gcd(y,x%y);}
ll ksm(ll a,ll b){ll r=1;while(b){if(b&1) r=(r*a)%mod;a=a*a%mod;b>>=1;}return r;}
int n,m,a[maxn]={0},b[maxn]={0};
int judge(int mid)
{
	memcpy(b,a,sizeof(a));
	f(i,n)
	{
		if(b[i]<b[i-1])
		{
			if(b[i]+mid<b[i-1]) return 0;
			b[i]=b[i-1];
		}
		else if(b[i]>=b[i-1])
		{
			if(b[i]+mid-m>=b[i-1]) b[i]=b[i-1];//继承上一状态的值，越小越好
		}
	}
	return 1;
}
int main()
{
	fast;cin>>n>>m;
	f(i,n) cin>>a[i];
	int l=0,r=1e9,ans;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(judge(mid))
		{
			ans=mid;
			r=mid-1;
		}
		else l=mid+1;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(int i=1;i<=n;i++)
#define mm make_pair
#define pi 2*acos(0.0)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define Case(i,ans) cout<<"Case "<<i<<": "<<ans<<endl;
#define cp(i,ans) printf("%.if",ans);
#define cpp(i,ans) cout.precision(i);cout<<fixed<<ans<<endl;
#define Y {cout<<"YES"<<endl;return 0;}
#define N {cout<<"NO"<<endl;return 0;}
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int inf=0x3f3f3f,maxn=3e5;
ll gcd(ll x,ll y){if(y==0) return x;else return gcd(y,x%y);}
ll ksm(ll a,ll b){ll r=1;while(b){if(b&1) r=(r*a)%mod;a=a*a%mod;b>>=1;}return r;}
int n,m,a[maxn]={0},b[maxn]={0};
int judge(int mid)
{
	memcpy(b,a,sizeof(a));
	f(i,n)
	{
		if(b[i]>b[i-1])
		{
			if(b[i]+mid-m>=b[i-1]) b[i]=b[i-1];//继承上一状态的值，越小越好
		}
		if(b[i]<b[i-1])
		{
			if(b[i]+mid<b[i-1]) return 0;
			else b[i]=b[i-1];
		}
	}
	return 1;
}
int main()
{
	fast;cin>>n>>m;
	f(i,n) cin>>a[i];
	int l=0,r=m+10,ans;
	while(l<=r)
	{
		int m=(l+r)>>1;
		if(judge(m))
		{
			ans=m;
			r=m-1;
		}
		else l=m+1;
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX_N=3e5+5;
int n,m;
int arr[MAX_N];
int arr0[MAX_N];
bool f(int x){
    for(int i=0;i<n;i++)    arr0[i]=arr[i];
    if(m-arr0[0]<=x) arr0[0]=0;
    for(int i=1;i<n;i++){
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]>x)  return 0;
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]<=x) arr0[i]=arr0[i-1];
        if(arr0[i-1]<arr0[i]&&m+arr0[i-1]-arr0[i]<=x)   arr0[i]=arr0[i-1];
    }
    return 1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++)    cin>>arr[i];

    int L=0,R=300000;

    while(L!=R){
        int mid=(L+R)/2;
        if(f(mid))  R=mid;
        else    L=mid+1;
    }
    cout<<L<<endl;
    //cout<<f(5)<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX_N=3e5+5;
int n,m;
int arr[MAX_N];
int arr0[MAX_N];
bool f(int x){
    for(int i=0;i<m;i++)    arr0[i]=arr[i];
    if(m-arr0[0]<=x) arr0[0]=0;
    for(int i=1;i<n;i++){
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]>x)  return 0;
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]<=x) arr0[i]=arr0[i-1];
        if(arr0[i-1]<arr0[i]&&m+arr0[i-1]-arr0[i]<=x)   arr0[i]=arr0[i-1];
    }
    return 1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++)    cin>>arr[i];

    int L=0,R=300000;

    while(L!=R){
        int mid=(L+R)/2;
        if(f(mid))  R=mid;
        else    L=mid+1;
    }
    cout<<L<<endl;
    //cout<<f(5)<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX_N=3e5+5;
int n,m;
int arr[MAX_N];
int arr0[MAX_N];
bool f(int x){
    for(int i=0;i<m;i++)    arr0[i]=arr[i];
    if(m-arr0[0]<=x) arr0[0]=0;
    for(int i=1;i<n;i++){
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]>x)  return 0;
        if(arr0[i-1]<arr0[i]&&m+arr0[i-1]-arr0[i]<=x)   arr0[i]=arr0[i-1];
    }
    return 1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++)    cin>>arr[i];

    int L=0,R=300000;

    while(L!=R){
        int mid=(L+R)/2;
        if(f(mid))  R=mid;
        else    L=mid+1;
    }
    cout<<L<<endl;
    //cout<<f(0)<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX_N=3e5+5;
int n,m;
int arr[MAX_N];
int arr0[MAX_N];
bool f(int x){
    for(int i=0;i<m;i++)    arr0[i]=arr[i];
    if(m-arr0[0]<=x) arr0[0]=0;
    for(int i=1;i<n;i++){
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]>x)  return 0;
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]<=x) arr0[i]=arr0[i-1];
        if(arr0[i-1]<arr0[i]&&m+arr0[i-1]-arr0[i]<x)   arr0[i]=arr0[i-1];
    }
    return 1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++)    cin>>arr[i];

    int L=0,R=300000;

    while(L!=R){
        int mid=(L+R)/2;
        if(f(mid))  R=mid;
        else    L=mid+1;
    }
    cout<<L<<endl;
    //cout<<f(5)<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAX_N=3e5+5;
int n,m;
int arr[MAX_N];
int arr0[MAX_N];
bool f(int x){
    for(int i=0;i<m;i++)    arr0[i]=arr[i];
    if(m-arr0[0]<=x) arr0[0]=0;
    for(int i=1;i<n;i++){
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]>x)  return 0;
        if(arr0[i-1]>arr0[i]&&arr0[i-1]-arr0[i]<=x) arr0[i]=arr0[i-1];
        if(arr0[i-1]<arr0[i]&&m+arr0[i-1]-arr0[i]<=x)   arr0[i]=arr0[i-1];
    }
    return 1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<n;i++)    cin>>arr[i];

    int L=0,R=300005;

    while(L!=R){
        int mid=(L+R)/2;
        if(f(mid))  R=mid;
        else    L=mid+1;
    }
    cout<<L<<endl;
    //cout<<f(5)<<endl;
}


#include <bits/stdc++.h>

#define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define PB push_back
#define MP make_pair
#define INF (int)1e9+7
#define LLINF 4e18+7
#define ALL(a) a.begin(), a.end()
#define SIZE(a) (int)a.size()

typedef long long LL;
typedef long double LD;

using namespace std;

const int MAX = 3 * 1e5 + 7;
int arr[MAX];
int DP[MAX][20];
int prevv[20];

int main()
{
    BOOST;

    int n, q;

    cin >> n >> q;

    FOR(i, 1, n + 1)
    {
        cin >> arr[i];
    }

    FOR(i, 0, 21)
    {
        prevv[i] = INF;
    }

    for(int i = n; i > 0; --i)
    {
        FOR(j, 0, 20)
        {
            DP[i][j] = INF;
        }

        FOR(j, 0, 21)
        {
            if(arr[i] & (1 << j))
            {
                DP[i][j] = prevv[j];

                if(prevv[j] != INF)
                {
                    FOR(z, 0, 21)
                    {
                        DP[i][z] = min(DP[i][z], DP[prevv[j]][z]);
                    }
                }

                prevv[j] = i;
            }
        }
    }

    FOR(i, 0, q)
    {
        int a, b;

        cin >> a >> b;

        bool works = false;

        FOR(i, 0, 20)
        {
            if(arr[b] & (1 << i) && DP[a][i] <= b)
            {
                works = true;
            }
        }

        if(works)
        {
            cout << "Shi\n";
        }
        else
        {
            cout << "Fou\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define PB push_back
#define MP make_pair
#define INF (int)1e9+7
#define LLINF 4e18+7
#define ALL(a) a.begin(), a.end()
#define SIZE(a) (int)a.size()

typedef long long LL;
typedef long double LD;

using namespace std;

const int MAX = 3 * 1e5 + 7;
int arr[MAX];
int DP[MAX][21];
int prevv[21];

int main()
{
    BOOST;

    int n, q;

    cin >> n >> q;

    FOR(i, 1, n + 1)
    {
        cin >> arr[i];
    }

    FOR(i, 0, 21)
    {
        prevv[i] = INF;
    }

    for(int i = n; i > 0; --i)
    {
        FOR(j, 0, 21)
        {
            DP[i][j] = INF;
        }

        FOR(j, 0, 21)
        {
            if(arr[i] & (1 << j))
            {
                DP[i][j] = i;

                if(prevv[j] != INF)
                {
                    FOR(z, 0, 21)
                    {
                        DP[i][z] = min(DP[i][z], DP[prevv[j]][z]);
                    }
                }

                prevv[j] = i;
            }
        }
    }

    FOR(i, 0, q)
    {
        int a, b;

        cin >> a >> b;

        bool works = false;

        FOR(i, 0, 21)
        {
            if((arr[b] & (1 << i)) && DP[a][i] <= b)
            {
                works = true;
            }
        }

        if(works)
        {
            cout << "Shi\n";
        }
        else
        {
            cout << "Fou\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define PB push_back
#define MP make_pair
#define INF (int)1e9+7
#define LLINF 4e18+7
#define ALL(a) a.begin(), a.end()
#define SIZE(a) (int)a.size()

typedef long long LL;
typedef long double LD;

using namespace std;

const int MAX = 3 * 1e5 + 7;
int arr[MAX];
int DP[MAX][20];
int prevv[20];

int main()
{
    BOOST;

    int n, q;

    cin >> n >> q;

    FOR(i, 1, n + 1)
    {
        cin >> arr[i];
    }

    FOR(i, 0, 21)
    {
        prevv[i] = INF;
    }

    for(int i = n; i > 0; --i)
    {
        FOR(j, 0, 20)
        {
            DP[i][j] = INF;
        }

        FOR(j, 0, 21)
        {
            if(arr[i] & (1 << j))
            {
                DP[i][j] = i;

                if(prevv[j] != INF)
                {
                    FOR(z, 0, 21)
                    {
                        DP[i][z] = min(DP[i][z], DP[prevv[j]][z]);
                    }
                }

                prevv[j] = i;
            }
        }
    }

    FOR(i, 0, q)
    {
        int a, b;

        cin >> a >> b;

        bool works = false;

        FOR(i, 0, 20)
        {
            if(arr[b] & (1 << i) && DP[a][i] <= b)
            {
                works = true;
            }
        }

        if(works)
        {
            cout << "Shi\n";
        }
        else
        {
            cout << "Fou\n";
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int n;
int a[300000];
int dyn[300000][19];
int nextset[300000][19];

int nextreachable(int p, int m) {
    // cout << p << " " << m << "\n";
    if (p >= n) {
        return p;
    }
    if (a[p] & (1 << m)) {
        return p;
    }
    if (dyn[p][m] == -1) {
        int mp = n;
        for (int i = 0; i < 19; ++i) {
            if (a[p] & (1 << i)) {
                int pc = nextset[p + 1][i];
                mp = min(mp, nextreachable(pc, m));
            }
        }
        dyn[p][m] = mp;
    }
    return dyn[p][m];
}

int main() {
    int q;
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        for (int j = 0; j < 19; ++j) {
            dyn[i][j] = -1;
        }
    }
    for (int i = n - 1; i >= 0; --i) {
        for (int j = 0; j < 19; ++j) {
            if (a[i] & (1 << j)) {
                nextset[i][j] = i;
            } else if (i == n - 1) {
                nextset[i][j] = n;
            } else {
                nextset[i][j] = nextset[i + 1][j];
            }
            // cout << nextset[i][j] << " ";
        }
        // cout << endl;
    }

    for (int i = 0; i < q; ++i) {
        int x, y;
        cin >> x >> y;
        --x; --y;

        for (int j = 0; j < 19; ++j) {
            if (a[y] & (1 << j)) {
                int mi = nextreachable(x, j);
                if (mi <= y) {
                    cout << "Shi" << endl;
                    goto qloop;
                }
            }
        }
        cout << "Fou" << endl;
qloop:;
    }
}


#include <iostream>
#include <algorithm>
using namespace std;

int n;
int a[300000];
int dyn[300000][19];
int nextset[300000][19];

int main() {
    int q;
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        for (int j = 0; j < 19; ++j) {
            dyn[i][j] = -1;
        }
    }
    for (int i = n - 1; i >= 0; --i) {
        for (int j = 0; j < 19; ++j) {
            if (a[i] & (1 << j)) {
                nextset[i][j] = i;
            } else if (i == n - 1) {
                nextset[i][j] = n;
            } else {
                nextset[i][j] = nextset[i + 1][j];
            }
            // cout << nextset[i][j] << " ";
        }
        // cout << endl;
    }

    for (int p = n - 1; p >= 0; --p) {
        for (int m = 0; m < 19; ++m) {
            if (a[p] & (1 << m)) {
                dyn[p][m] = p;
            } else {
                int mp = n;
                for (int i = 0; i < 19; ++i) {
                    if (a[p] & (1 << i)) {
                        int pc = nextset[p + 1][i];
                        if (pc < n) {
                            mp = min(mp, dyn[pc][m]);
                        }
                    }
                }
                dyn[p][m] = mp;
            }
        }
    }

    for (int i = 0; i < q; ++i) {
        int x, y;
        cin >> x >> y;
        --x; --y;

        for (int j = 0; j < 19; ++j) {
            if (a[y] & (1 << j)) {
                // int mi = nextreachable(x, j);
                int mi = dyn[x][j];
                if (mi <= y) {
                    cout << "Shi" << endl;
                    goto qloop;
                }
            }
        }
        cout << "Fou" << endl;
qloop:;
    }
}


#include <iostream>
using namespace std;

int n;
int a[300000];
int dyn[300000][19];

int nextwithset(int p, int m) {
    if (p >= n) {
        return p;
    }
    if (a[p] & (1 << m)) {
        return p;
    }
    if (dyn[p][m] == -1) {
        int mp = n;
        for (int i = 0; i < 19; ++i) {
            if (a[p] & (1 << i)) {
                int pc = nextwithset(p + 1, i);
                mp = min(mp, nextwithset(pc, m));
            }
        }
        dyn[p][m] = mp;
    }
    return dyn[p][m];
}

int main() {
    int q;
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    fill_n(&dyn[0][0], 300000 * 19, -1);
    for (int i = 0; i < q; ++i) {
        int x, y;
        cin >> x >> y;
        --x; --y;

        for (int j = 0; j < 19; ++j) {
            if (a[y] & (1 << j)) {
                int mi = nextwithset(x, j);
                if (mi <= y) {
                    cout << "Shi" << endl;
                    goto qloop;
                }
            }
        }
        cout << "Fou" << endl;
qloop:;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 20;
    vector<vector<vector<ll>>> c((size_t) n, vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n)));
    vector<vector<vector<ll>>> d((size_t) n, vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n)));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        for (ll j = 0; j < m; j++) {
            if (i != n - 1) {
                e[i][j] = e[i + 1][j];
            }
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[i][j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if (i != n - 1) {
                        c[i][j][k] = d[i + 1][j][k];
                    }
                }
                d[i][j][k] = i == n - 1 ? c[i][j][k] : min(c[i][j][k], d[i + 1][j][k]);
                e[i][j] = min(e[i][j], c[i][j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                    if ((a[i] & (1 << j)) != 0) {
                        x[k] = min(x[k], c[j][k]);
                    }
                    if ((a[i] & (1 << k)) != 0) {
                        x[j] = min(x[j], c[j][k]);
                    }
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) == 0) {
                    c[j][k] = min(c[j][k], x[k]);
                }
                if ((a[i] & (1 << k)) != 0 && (a[i] & (1 << j)) == 0) {
                    c[j][k] = min(c[j][k], x[j]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 20;
    vector<vector<vector<ll>>> c((size_t) n, vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n)));
    vector<vector<vector<ll>>> d((size_t) n, vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n)));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        for (ll j = 0; j < m; j++) {
            if (i != n - 1) {
                e[i][j] = e[i + 1][j];
            }
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[i][j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if (i != n - 1) {
                        c[i][j][k] = d[i + 1][j][k];
                    }
                }
                d[i][j][k] = i == n - 1 ? c[i][j][k] : min(c[i][j][k], d[i + 1][j][k]);
                e[i][j] = min(e[i][j], c[i][j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                if ((a[i] & (1 << j)) != 0) {
                    x[k] = min(x[k], c[j][k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    x[j] = min(x[j], c[j][k]);
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                c[j][k] = min(c[j][k], x[j]);
                c[j][k] = min(c[j][k], x[k]);
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                if ((a[i] & (1 << j)) != 0) {
                    x[k] = min(x[k], c[j][k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    x[j] = min(x[j], c[j][k]);
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = min(c[j][k], x[j]);
                    c[j][k] = min(c[j][k], x[k]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                    if ((a[i] & (1 << j)) != 0) {
                        x[k] = min(x[k], c[j][k]);
                    }
                    if ((a[i] & (1 << k)) != 0) {
                        x[j] = min(x[j], c[j][k]);
                    }
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0) {
                    c[j][k] = min(c[j][k], x[k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    c[j][k] = min(c[j][k], x[j]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                if ((a[i] & (1 << j)) != 0) {
                    x[k] = min(x[k], c[j][k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    x[j] = min(x[j], c[j][k]);
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0) {
                    c[j][k] = min(c[j][k], x[k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    c[j][k] = min(c[j][k], x[j]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                if ((a[i] & (1 << j)) != 0) {
                    x[k] = min(x[k], c[j][k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    x[j] = min(x[j], c[j][k]);
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                c[j][k] = min(c[j][k], x[j]);
                c[j][k] = min(c[j][k], x[k]);
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][j], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 20;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if (i != n - 1) {
                        c[j][k] = d[j][k];
                    }
                }
                e[i][j] = min(e[i][j], c[j][k]);
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 4;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                if ((a[i] & (1 << j)) != 0) {
                    x[k] = min(x[k], c[j][k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    x[j] = min(x[j], c[j][k]);
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                c[j][k] = min(c[j][k], x[j]);
                c[j][k] = min(c[j][k], x[k]);
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][j], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if ((a[i] & (1 << j)) != 0) {
                        c[j][k] = d[j][k];
                    }
                    if ((a[i] & (1 << j)) != 0) {
                        x[k] = min(x[k], c[j][k]);
                    }
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) == 0) {
                    c[j][k] = min(c[j][k], x[k]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                    break;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if ((a[i] & (1 << j)) != 0) {
                        c[j][k] = d[j][k];
                    }
                    if ((a[i] & (1 << j)) != 0) {
                        x[k] = min(x[k], c[j][k]);
                    }
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) == 0) {
                    c[j][k] = min(c[j][k], x[k]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                    break;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 20;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                if ((a[i] & (1 << j)) != 0) {
                    x[k] = min(x[k], c[j][k]);
                }
                if ((a[i] & (1 << k)) != 0) {
                    x[j] = min(x[j], c[j][k]);
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                c[j][k] = min(c[j][k], x[j]);
                c[j][k] = min(c[j][k], x[k]);
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][j], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    c[j][k] = d[j][k];
                }
                e[i][j] = min(e[i][j], c[j][k]);
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 20;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        for (ll j = 0; j < m; j++) {
            if (i != n - 1) {
                e[i][j] = e[i + 1][j];
            }
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if (i != n - 1) {
                        c[j][k] = d[j][k];
                    }
                }
                e[i][j] = min(e[i][j], c[j][k]);
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, q;
    cin >> n >> q;
    vector<ll> a((size_t) n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<pair<ll, ll>> b((size_t) q);
    for (ll i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    ll m = 22;
    vector<vector<ll>> c((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> d((size_t) m, vector<ll>((size_t) m, n));
    vector<vector<ll>> e((size_t) n, vector<ll>((size_t) m, n));
    for (ll i = n - 1; i >= 0; i--) {
        c = vector<vector<ll>>((size_t) m, vector<ll>((size_t) m, n));
        vector<ll> x((size_t) m, n);
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {
                    c[j][k] = i;
                } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {
                    if ((a[i] & (1 << j)) != 0) {
                        c[j][k] = d[j][k];
                    }
                    if ((a[i] & (1 << j)) != 0) {
                        x[k] = min(x[k], c[j][k]);
                    }
                }
            }
        }
        for (ll j = 0; j < m; j++) {
            for (ll k = 0; k < m; k++) {
                if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) == 0) {
                    c[j][k] = min(c[j][k], x[k]);
                }
                e[i][j] = min(e[i][j], c[j][k]);
                e[i][k] = min(e[i][k], c[j][k]);
                d[j][k] = min(d[j][k], c[j][k]);
            }
        }
    }
    for (ll i = 0; i < q; i++) {
        bool f = false;
        for (ll j = 0; j < m; j++) {
            if ((a[b[i].second] & (1 << j)) != 0) {
                if (e[b[i].first][j] <= b[i].second) {
                    f = true;
                }
            }
        }
        cout << (f ? "Shi" : "Fou") << endl;
    }
}