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.util.*;
  import java.lang.*;
  import java.io.*;
  
  public final class Solution{
    static ArrayList<String> list= new ArrayList<>();
   
   static void prem( String s ,int i, boolean[] vist, String ans){
        if(i==s.length()){
          //    System.out.println(ans);
            list.add(ans);
            return;
        }
         String str="abc";
         for(int j=0;j<3;j++){
        
          if(!vist[j]){
            
            ans+=str.charAt(j);
            
            vist[j]=true;
            
            prem(s,i+1,vist,ans);
            
            vist[j]=false;
                
            ans = ans.substring(0,ans.length()-1);  
            }
  
         }
       
       }
      
     public static void main (String[] args) throws Exception
     {  
      
        BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
       
             Scanner sc= new Scanner(System.in);
              
              String s="abc";
              boolean[] vist= new boolean[3];
              prem(s,0,vist,"");
              int n=sc.nextInt();
              int m=sc.nextInt();
              String in=sc.next();
              char[] arr= new char[n];
              for(int i=0;i<n;i++){
                  arr[i]=in.charAt(i);
              }
              int[][] dp= new int[list.size()][n+1];
              for(int i=0;i<list.size();i++){
                   int count=0;
                   String str=list.get(i);
                   int ind=0;
                   for(int j=0;j<n;j++){
                       if(ind>2){ ind=0;}
                       if(arr[j]!=str.charAt(ind)){
                           count++;
                       }
                       ind++;
                       dp[i][j+1]=count;
                   }
              }
              //  debug.print2d(dp);
               while(m-->0){
                   int l=sc.nextInt();
                   int r=sc.nextInt();
                   
                   
                    int min=Integer.MAX_VALUE;
                    for(int i=0;i<list.size();i++){
                        int val=dp[i][r]-dp[i][l-1];
                        min=Math.min(min,val);
                    }
                    System.out.println(min);
               }
  
  
  
        
             
  
                
              
            
            
           
               
               op.flush();
  
  
     }
  
  
   
   
   
   }
  
  //  int l=sc.nextInt();
  //                 int r=sc.nextInt();
  //                 l--;
  //                 r--;
  //                 char[] sub=new char[(r-l)+1];
                  
  //                 int c=0;
  //                 for(int i=l;i<=r;i++){
  //                     sub[c]=arr[i];
  //                     c++;
  //                 }
  //                 int min=Integer.MAX_VALUE;
               
  //                 for(int i=0;i<list.size();i++){
  //                     int count=0;
  //                     String pr=list.get(i);
  //                     int ind=0;
                        
  //                     for(int j=0;j<(r-l)+1;j++){
  //                         if(ind>2){ ind = 0;}
  //                          if(sub[j] != pr.charAt(ind)){
                             
  //                              count++;
                             
  //                          }
  //                          ind++;
                         
  //                     }
                     
  //                     min = Math.min(count,min);
  //                 }
  //                 op.write(min+"\n");
  
   class debug{
           
      
     static void print1d(long [] arr){
         System.out.println();
         for(int i=0;i<arr.length;i++){
             System.out.print(arr[i]+" ");
         }
         System.out.println();
     }
       static void print1d(int [] arr){
         System.out.println();
         for(int i=0;i<arr.length;i++){
             System.out.print(arr[i]+" ");
         }
         System.out.println();
     }
     
      static void print2d(int[][] arr){
          System.out.println();
          int n=arr.length;
          int n2=arr[0].length;
          for(int i=0;i<n;i++){
              for(int j=0;j<n2;j++){
                  System.out.print(arr[i][j]+" ");
              }
              System.out.println();
          }
          System.out.println();
      }
       static void print2d(long[][] arr){
          System.out.println();
          int n=arr.length;
          int n2=arr[0].length;
          for(int i=0;i<n;i++){
              for(int j=0;j<n2;j++){
                  System.out.print(arr[i][j]+" ");
              }
              System.out.println();
          }
          System.out.println();
      }
  
  
  
   }
    
  
  
    class pair{
        long no;
        int ind;
        pair(long no, int ind){
            this.no=no;
            this.ind=ind;
        }
    }
    class comp implements Comparator<pair>{
        public int compare(pair p1, pair p2){
            if(p1.no>p2.no){
                return 1;
            }
            return -1;
        }
    }
  
   
   class Reader {
          final private int BUFFER_SIZE = 1 << 16;
          private DataInputStream din;
          private byte[] buffer;
          private int bufferPointer, bytesRead;
   
          public Reader()
          {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
   
          public Reader(String file_name) throws IOException
          {
              din = new DataInputStream(
                  new FileInputStream(file_name));
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
   
          public String readLine() throws IOException
          {
              byte[] buf = new byte[64]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n') {
                      if (cnt != 0) {
                          break;
                      }
                      else {
                          continue;
                      }
                  }
                  buf[cnt++] = (byte)c;
              }
              return new String(buf, 0, cnt);
          }
   
          public int nextInt() throws IOException
          {
              int ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
   
              if (neg)
                  return -ret;
              return ret;
          }
   
          public long nextLong() throws IOException
          {
              long ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
              if (neg)
                  return -ret;
              return ret;
          }
   
          public double nextDouble() throws IOException
          {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
   
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
   
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
   
              if (neg)
                  return -ret;
              return ret;
          }
   
          private void fillBuffer() throws IOException
          {
              bytesRead = din.read(buffer, bufferPointer = 0,
                                   BUFFER_SIZE);
              if (bytesRead == -1)
                  buffer[0] = -1;
          }
   
          private byte read() throws IOException
          {
              if (bufferPointer == bytesRead)
                  fillBuffer();
              return buffer[bufferPointer++];
          }
   
          public void close() throws IOException
          {
              if (din == null)
                  return;
              din.close();
          }
      }
  
      
  
  //   BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
  
  
  // in comparator to sort in in order return -1 
  // in comparator to sort in increasing order return 1
  
  
  // sorting in increasing 
  // class Comp implements Comparator<pair>{
  //     public int compare(pair a,pair b){
  //         if(a.b>b.b){
  //             return 1;    here
  //         }
  //         return -1;
  //     }
  // }  
  
  
  
  