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.*;
  public class SayNoToPalindromes {
      static class InputReader {
          private static final int DEFAULT_BUFFER_SIZE = 1 << 16; // Change this to increase your input size //
          private static final InputStream DEFAULT_STREAM = System.in;
          private static final int MAX_DECIMAL_PRECISION = 21;
          private int c;
          private byte[] buf;
          private int bufferSize, bufIndex, numBytesRead;
          private InputStream stream;
          private static final byte EOF = -1;
          private static final byte NEW_LINE = 10;
          private static final byte CARRIAGE_RETURN = 13;
          private static final byte SPACE = 32;
          private static final byte DASH = 45;
          private static final byte DOT = 46;
          private char[] charBuffer;
          private static byte[] bytes = new byte[58];
          private static int[] ints = new int[58];
          private static char[] chars = new char[128];
  
          static {
              char ch = ' ';
              int value = 0;
              byte _byte = 0;
              for (int i = 48; i < 58; i++) bytes[i] = _byte++;
              for (int i = 48; i < 58; i++) ints[i] = value++;
              for (int i = 32; i < 128; i++) chars[i] = ch++;
          }
  
          public InputReader() {
              this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);
          }
  
          public InputReader(int bufferSize) {
              this(DEFAULT_STREAM, bufferSize);
          }
  
          public InputReader(InputStream stream) {
              this(stream, DEFAULT_BUFFER_SIZE);
          }
  
          public InputReader(InputStream stream, int bufferSize) {
              if (stream == null || bufferSize <= 0) throw new IllegalArgumentException();
              buf = new byte[bufferSize];
              charBuffer = new char[128];
              this.bufferSize = bufferSize;
              this.stream = stream;
          }
  
          private byte read() throws IOException {
              if (numBytesRead == EOF) throw new IOException();
              if (bufIndex >= numBytesRead) {
                  bufIndex = 0;
                  numBytesRead = stream.read(buf);
                  if (numBytesRead == EOF) return EOF;
              }
              return buf[bufIndex++];
          }
  
          private int readJunk(int token) throws IOException {
              if (numBytesRead == EOF) return EOF;
              do {
                  while (bufIndex < numBytesRead) {
                      if (buf[bufIndex] > token) return 0;
                      bufIndex++;
                  }
                  numBytesRead = stream.read(buf);
                  if (numBytesRead == EOF) return EOF;
                  bufIndex = 0;
              } while (true);
          }
  
          public byte nextByte() throws IOException {
              return (byte) ni();
          }
  
          public int ni() throws IOException {
              if (readJunk(DASH - 1) == EOF) throw new IOException();
              int sgn = 1, res = 0;
              c = buf[bufIndex];
              if (c == DASH) {
                  sgn = -1;
                  bufIndex++;
              }
              do {
                  while (bufIndex < numBytesRead) {
                      if (buf[bufIndex] > SPACE) {
                          res = (res << 3) + (res << 1);
                          res += ints[buf[bufIndex++]];
                      } else {
                          bufIndex++;
                          return res * sgn;
                      }
                  }
                  numBytesRead = stream.read(buf);
                  if (numBytesRead == EOF) return res * sgn;
                  bufIndex = 0;
              } while (true);
          }
  
          public long nl() throws IOException {
              if (readJunk(DASH - 1) == EOF) throw new IOException();
              int sgn = 1;
              long res = 0L;
              c = buf[bufIndex];
              if (c == DASH) {
                  sgn = -1;
                  bufIndex++;
              }
              do {
                  while (bufIndex < numBytesRead) {
                      if (buf[bufIndex] > SPACE) {
                          res = (res << 3) + (res << 1);
                          res += ints[buf[bufIndex++]];
                      } else {
                          bufIndex++;
                          return res * sgn;
                      }
                  }
                  numBytesRead = stream.read(buf);
                  if (numBytesRead == EOF) return res * sgn;
                  bufIndex = 0;
              } while (true);
          }
  
          private void doubleCharBufferSize() {
              char[] newBuffer = new char[charBuffer.length << 1];
              for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];
              charBuffer = newBuffer;
          }
  
          public String nli() throws IOException {
              try {
                  c = read();
              } catch (IOException e) {
                  return null;
              }
              if (c == NEW_LINE) return "";
              if (c == EOF) return null;
              int i = 0;
              charBuffer[i++] = (char) c;
              do {
                  while (bufIndex < numBytesRead) {
                      if (buf[bufIndex] != NEW_LINE && buf[bufIndex] != CARRIAGE_RETURN) {
                          if (i == charBuffer.length) doubleCharBufferSize();
                          charBuffer[i++] = (char) buf[bufIndex++];
                      } else {
                          if (buf[bufIndex] == CARRIAGE_RETURN) bufIndex++;
                          bufIndex++;
                          return new String(charBuffer, 0, i);
                      }
                  }
                  numBytesRead = stream.read(buf);
                  if (numBytesRead == EOF) return new String(charBuffer, 0, i);
                  bufIndex = 0;
              } while (true);
          }
  
          public String ns() throws IOException {
              if (numBytesRead == EOF) return null;
              if (readJunk(SPACE) == EOF) return null;
              for (int i = 0; ; ) {
                  while (bufIndex < numBytesRead) {
                      if (buf[bufIndex] > SPACE) {
                          if (i == charBuffer.length) doubleCharBufferSize();
                          charBuffer[i++] = (char) buf[bufIndex++];
                      } else {
                          bufIndex++;
                          return new String(charBuffer, 0, i);
                      }
                  }
                  numBytesRead = stream.read(buf);
                  if (numBytesRead == EOF) return new String(charBuffer, 0, i);
                  bufIndex = 0;
              }
          }
  
          public double nd() throws IOException {
              String doubleVal = ns();
              if (doubleVal == null) throw new IOException();
              return Double.valueOf(doubleVal);
          }
  
          public void close() throws IOException {
              stream.close();
          }
      }
  
      // region variables
      static InputReader sc = new InputReader();
      static OutputStream outputStream = System.out;
      static PrintWriter w = new PrintWriter(outputStream);
      // endregion
  
      private static void initiateIO()
              throws IOException {
          if (System.getProperty("ONLINE_JUDGE") == null) {
              try {
                  w = new PrintWriter("output.txt");
                  sc = new InputReader(new FileInputStream("input.txt"));
              } catch (Exception e) {
                  throw new IOException();
              }
          }
      }
  
      public static void main(String[] args)
              throws IOException {
  
          initiateIO();
  
  //        int t = sc.ni();
  //        while (t-- > 0)
              solve();
  
          w.close();
  
      }
  
      static void solve() throws IOException {
          int n = sc.ni();
          int m = sc.ni();
          String input = sc.ns();
          String a = "abc";
          StringBuilder sb = new StringBuilder(a);
          while (sb.length() < n) {
              sb.append(sb);
          }
          a = sb.substring(0, n);
          String b = "bca";
          sb = new StringBuilder(b);
          while (sb.length() < n) {
              sb.append(sb);
          }
          b = sb.substring(0, n);
          String c = "cab";
          sb = new StringBuilder(c);
          while (sb.length() < n) {
              sb.append(sb);
          }
          c = sb.substring(0, n);
          String d = "acb";
          sb = new StringBuilder(d);
          while (sb.length() < n) {
              sb.append(sb);
          }
          d = sb.substring(0, n);
          String e = "cba";
          sb = new StringBuilder(e);
          while (sb.length() < n) {
              sb.append(sb);
          }
          e = sb.substring(0, n);
          String f = "bac";
          sb = new StringBuilder(f);
          while (sb.length() < n) {
              sb.append(sb);
          }
          f = sb.substring(0, n);
  
          int[][] results = new int[6][n];
  
          check(results[0], a.toCharArray(), input.toCharArray());
          check(results[1], b.toCharArray(), input.toCharArray());
          check(results[2], c.toCharArray(), input.toCharArray());
          check(results[3], d.toCharArray(), input.toCharArray());
          check(results[4], e.toCharArray(), input.toCharArray());
          check(results[5], f.toCharArray(), input.toCharArray());
  
          while(m-- > 0) {
              int l = sc.ni();
              int r = sc.ni();
              l--;
              r--;
              int min = Integer.MAX_VALUE;
              for(int i = 0; i < 6; i++) {
                  if(l == 0) min = Math.min(min, results[i][r]);
                  else min = Math.min(min, results[i][r]-results[i][l-1]);
              }
              w.println(min);
          }
      }
  
      static void check (int[] arr, char[] s, char[] in) {
          if(s[0] != in[0]) arr[0] = 1;
          for(int i = 1; i < s.length; i++) {
              if(s[i] != in[i]) {
                  arr[i] = 1 + arr[i-1];
              } else arr[i] = arr[i-1];
          }
      }
  }
  