description: Let's call the string beautiful if it does not contain a substring of length at least 2, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.
  
  Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 3 letters of the Latin alphabet (in lowercase).
  
  You are given a string s of length n, each character of the string is one of the first 3 letters of the Latin alphabet (in lowercase).
  
  You have to answer m queries — calculate the cost of the substring of the string s from l_i-th to r_i-th position, inclusive.
  
  Input
  
  The first line contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the length of the string s and the number of queries.
  
  The second line contains the string s, it consists of n characters, each character one of the first 3 Latin letters.
  
  The following m lines contain two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — parameters of the i-th query.
  
  Output
  
  For each query, print a single integer — the cost of the substring of the string s from l_i-th to r_i-th position, inclusive.
  
  Example
  
  Input
  
  
  5 4
  baacb
  1 3
  1 5
  4 5
  2 3
  
  
  Output
  
  
  1
  2
  0
  1
  
  Note
  
  Consider the queries of the example test.
  
    * in the first query, the substring is baa, which can be changed to bac in one operation; 
    * in the second query, the substring is baacb, which can be changed to cbacb in two operations; 
    * in the third query, the substring is cb, which can be left unchanged; 
    * in the fourth query, the substring is aa, which can be changed to ba in one operation. 
solution: import java.io.*;
  import java.util.*;
  
  public class Main{
  	static void main() throws Exception{
  		int n=sc.nextInt(),m=sc.nextInt();
  		char[]in=sc.nextLine().toCharArray();
  		int[][]pref=new int[3][n];
  		for(int i=0;i<n;i++) {
  			pref[in[i]-'a'][i]++;
  			if(i>=3) {
  				for(int j=0;j<3;j++) {
  					pref[j][i]+=pref[j][i-3];
  				}
  			}
  		}
  		while(m-->0) {
  			int l=sc.nextInt()-1,r=sc.nextInt()-1;
  			if(l==r) {
  				pw.println(0);
  				continue;
  			}
  			if(l+1==r) {
  				pw.println(in[l]==in[r]?1:0);
  				continue;
  			}
  			int range=(r-l+1)/3;
  			int ans=n;
  			for(int f=0;f<3;f++) {
  				for(int s=0;s<3;s++) {
  					if(f==s)continue;
  					int th=0;
  					for(int o=0;o<3;o++) {
  						if(o!=f && o!=s) {
  							th=o;
  							break;
  						}
  					}
  					int curans=0;
  					int[]arr=new int[] {f,s,th};
  					for(int c=0,idx=r;c<3;c++,idx--) {
  						int curr=idx,curl=curr-range*3;
  						if(curl>=l) {
  							curl-=3;
  						}
  						curans+=pref[arr[(c+1)%3]][curr];
  						curans+=pref[arr[(c+2)%3]][curr];
  						if(curl>=0) {
  							curans-=pref[arr[(c+1)%3]][curl];
  							curans-=pref[arr[(c+2)%3]][curl];
  						}
  					}
  					ans=Math.min(ans, curans);
  				}
  			}
  			pw.println(ans);
  		}
  	}
  	public static void main(String[] args) throws Exception{
  		sc=new MScanner(System.in);
  		pw = new PrintWriter(System.out);
  		int tc=1;
  //        tc=sc.nextInt();
  		for(int i=1;i<=tc;i++) {
  //			pw.printf("Case #%d: ", i);
  			main();
  		}
  		pw.flush();
  	}
  	static PrintWriter pw;
  	static MScanner sc;
  	static class MScanner {
  		StringTokenizer st;
  		BufferedReader br;
  		public MScanner(InputStream system) {
  			br = new BufferedReader(new InputStreamReader(system));
  		}
  	 
  		public MScanner(String file) throws Exception {
  			br = new BufferedReader(new FileReader(file));
  		}
  	 
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  		public int[] intArr(int n) throws IOException {
  	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
  	        return in;
  		}
  		public long[] longArr(int n) throws IOException {
  	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
  	        return in;
  		}
  		public int[] intSortedArr(int n) throws IOException {
  	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
  	        shuffle(in);
  	        Arrays.sort(in);
  	        return in;
  		}
  		public long[] longSortedArr(int n) throws IOException {
  	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
  	        shuffle(in);
  	        Arrays.sort(in);
  	        return in;
  		}
  		public Integer[] IntegerArr(int n) throws IOException {
  	        Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
  	        return in;
  		}
  		public Long[] LongArr(int n) throws IOException {
  	        Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
  	        return in;
  		}
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  	 
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  	 
  		public double nextDouble() throws IOException {
  			return Double.parseDouble(next());
  		}
  	 
  		public char nextChar() throws IOException {
  			return next().charAt(0);
  		}
  	 
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  	 
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  	 
  		public void waitForInput() throws InterruptedException {
  			Thread.sleep(3000);
  		}
  		
  	}
  	static void dbg(int[]in) {
  		System.out.println(Arrays.toString(in));
  	}
  	static void dbg(long[]in) {
  		System.out.println(Arrays.toString(in));
  	}
  	static void sort(int[]in) {
  		shuffle(in);
  		Arrays.sort(in);
  	}
  	static void sort(long[]in) {
  		shuffle(in);
  		Arrays.sort(in);
  	}
  	static void shuffle(int[]in) {
  		for(int i=0;i<in.length;i++) {
  			int idx=(int)(Math.random()*in.length);
  			int tmp=in[i];
  			in[i]=in[idx];
  			in[idx]=tmp;
  		}
  	}
  	static void shuffle(long[]in) {
  		for(int i=0;i<in.length;i++) {
  			int idx=(int)(Math.random()*in.length);
  			long tmp=in[i];
  			in[i]=in[idx];
  			in[idx]=tmp;
  		}
  	}
  }