import java.util.*;
public class Main {
    public static void main(String[] args) {
        int seconds = 0, maxrate=0, result=0, maxon=0;
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Boolean[] state = new Boolean[n];
        Integer[] rate = new Integer[n], start = new Integer[n], round = new Integer[n];
        String s = scanner.nextLine();
        s = scanner.nextLine();
        char[] stateCh = new char[n];
        stateCh = s.toCharArray();
        for(int i = 0; i<n;i++)
            state[i] = stateCh[i] == '1' ? true : false;
        for(int i = 0; i<n;i++) {
            rate[i] = scanner.nextInt();
            start[i] = scanner.nextInt();
            round[i] = 1;
            maxrate = Math.max(maxrate, rate[i]);
        }

        while(seconds < maxrate*4*n + maxrate){
            for(int i = 0; i<state.length;i++){
                if(seconds >= start[i]){
                    if(seconds == start[i])
                        state[i] = state[i] == true ? state[i] = false : (state[i] = true);
                    else if(seconds == start[i] + rate[i]*round[i])
                    {
                        state[i] = state[i] == true ? state[i] = false : (state[i] = true);
                        round[i]++;
                    }
                }
                if(state[i]) maxon++;
            }
            result = Math.max(result, maxon);
            maxon = 0;
            seconds++;
        }
        System.out.println(result);
    }
}


public class Main {
    public static void main(String[] args) {
        int seconds = 0, maxrate=0, result=0, maxon=0;
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Boolean[] state = new Boolean[n];
        Integer[] rate = new Integer[n], start = new Integer[n], round = new Integer[n];
        String s = scanner.nextLine();
        s = scanner.nextLine();
        char[] stateCh = new char[n];
        stateCh = s.toCharArray();
        for(int i = 0; i<n;i++)
            state[i] = stateCh[i] == '1' ? true : false;
        for(int i = 0; i<n;i++) {
            rate[i] = scanner.nextInt();
            start[i] = scanner.nextInt();
            round[i] = 1;
            maxrate = Math.max(maxrate, rate[i]);
        }

        while(seconds < maxrate*2*n + maxrate){
            for(int i = 0; i<state.length;i++){
                if(seconds >= start[i]){
                    if(seconds == start[i])
                        state[i] = state[i] == true ? state[i] = false : (state[i] = true);
                    else if(seconds == start[i] + rate[i]*round[i])
                    {
                        state[i] = state[i] == true ? state[i] = false : (state[i] = true);
                        round[i]++;
                    }
                }
                if(state[i]) maxon++;
            }
            result = Math.max(result, maxon);
            maxon = 0;
            seconds++;
        }
        System.out.println(result);
        System.out.println(seconds);
    }
}


import java.util.*;
public class Main {
    public static void main(String[] args) {
        int seconds = 0, maxrate=0, maxdelay=0,result=0, maxon=0;
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Boolean[] state = new Boolean[n];
        Integer[] rate = new Integer[n], start = new Integer[n], round = new Integer[n];
        String s = scanner.nextLine();
        s = scanner.nextLine();
        char[] stateCh = new char[n];
        stateCh = s.toCharArray();
        for(int i = 0; i<n;i++)
            state[i] = stateCh[i] == '1' ? true : false;
        for(int i = 0; i<n;i++) {
            rate[i] = scanner.nextInt();
            start[i] = scanner.nextInt();
            round[i] = 1;
            maxrate = Math.max(maxrate, rate[i]);
            maxdelay = Math.max(maxdelay, start[i]);
        }

        while(seconds < maxrate*4*n + maxrate || seconds <= maxdelay){
            for(int i = 0; i<state.length;i++){
                if(seconds >= start[i]){
                    if(seconds == start[i])
                        state[i] = state[i] == true ? state[i] = false : (state[i] = true);
                    else if(seconds == start[i] + rate[i]*round[i])
                    {
                        state[i] = state[i] == true ? state[i] = false : (state[i] = true);
                        round[i]++;
                    }
                }
                if(state[i]) maxon++;
            }
            result = Math.max(result, maxon);
            maxon = 0;
            seconds++;
        }
        System.out.println(result);
    }
}


import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;

/* abhi2601 */

public class Q1 implements Runnable{

    final static long mod = (long)1e9 + 7;
    static class Pair implements Comparable<Pair>{
        int a;
        int b;
        Pair(int a,int b){
            this.a=a;
            this.b=b;
        }
        public int compareTo(Pair p){
            if(this.a-p.a==0) return this.b-p.b;
            return this.a-p.a;
        }
    }
    public void run(){
        InputReader sc = new InputReader(System.in);
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter w = new PrintWriter(System.out);
        int n=sc.nextInt();
        String s=sc.next();
        ArrayList<Pair>al=new ArrayList<>();
        for(int i=0;i<n;i++) al.add(new Pair(sc.nextInt(),sc.nextInt()));
        int ans=Integer.MIN_VALUE;
        for(int i=0;i<=10000;i++){
            int c=0;
            for(int j=0;j<n;j++){
                Pair p=al.get(j);
                if(p.b>i){
                    if(s.charAt(j)=='1') c++;
                }
                else{
                    int temp=i-(p.b-1);
                    int z=(int)Math.ceil((double)temp/p.a);
                    if(z%2==1){
                        if(s.charAt(j)=='0') c++;
                    }
                    else{
                        if(s.charAt(j)=='1') c++;
                    }
                }
            }
            ans=Math.max(ans,c);
            //w.println(i+" "+ans);
        }
        w.println(ans);
        w.close();
    }
    static class Comp implements Comparator<String>{
        public int compare(String x,String y){
            if(x.length()-y.length()==0){
                return x.compareTo(y);
            }
            return x.length()-y.length();
        }
    }
    //static PrintWriter w;
    /*File in=new File("input.txt");
        File out=new File("output.txt");
        Scanner sc= null;
        try {
            sc = new Scanner(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            w=new PrintWriter(out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new Q1(),"q1",1<<26).start();
    }
}


import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;

/* abhi2601 */

public class Q1 implements Runnable{

    final static long mod = (long)1e9 + 7;
    static class Pair implements Comparable<Pair>{
        int a;
        int b;
        Pair(int a,int b){
            this.a=a;
            this.b=b;
        }
        public int compareTo(Pair p){
            if(this.a-p.a==0) return this.b-p.b;
            return this.a-p.a;
        }
    }
    public void run(){
        InputReader sc = new InputReader(System.in);
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter w = new PrintWriter(System.out);
        int n=sc.nextInt();
        String s=sc.next();
        ArrayList<Pair>al=new ArrayList<>();
        for(int i=0;i<n;i++) al.add(new Pair(sc.nextInt(),sc.nextInt()));
        int ans=Integer.MIN_VALUE;
        for(int i=1;i<=200;i++){
            int c=0;
            for(int j=0;j<n;j++){
                Pair p=al.get(j);
                if(p.b>i){
                    if(s.charAt(j)=='1') c++;
                }
                else{
                    int temp=i-(p.b-1);
                    int z=(int)Math.ceil((double)temp/p.a);
                    if(z%2==1){
                        if(s.charAt(j)=='1') c++;
                    }
                    else{
                        if(s.charAt(j)=='0') c++;
                    }
                }
            }
            ans=Math.max(ans,c);
            //w.println(i+" "+ans);
        }
        w.println(ans);
        w.close();
    }
    static class Comp implements Comparator<String>{
        public int compare(String x,String y){
            if(x.length()-y.length()==0){
                return x.compareTo(y);
            }
            return x.length()-y.length();
        }
    }
    //static PrintWriter w;
    /*File in=new File("input.txt");
        File out=new File("output.txt");
        Scanner sc= null;
        try {
            sc = new Scanner(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            w=new PrintWriter(out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new Q1(),"q1",1<<26).start();
    }
}


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

public class B
{
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tok;

    public void go() throws IOException
    {
        ntok();
        int n = ipar();
        ntok();
        String s = spar();
        boolean[] arr = new boolean[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = s.charAt(i) == '1';
        }
        int[][] times = new int[n][2];
        for (int i = 0; i < n; i++)
        {
            ntok();
            times[i][0] = ipar();
            times[i][1] = ipar();
        }
        int max = 0;
        for (int t = 0; t < 100000; t++)
        {
            int on = 0;
            for (int i = 0; i < n; i++)
            {
                if (t - times[i][1] >= 0 && (t - times[i][1]) % times[i][0] == 0)
                {
                    arr[i] = !arr[i];
                }
                if (arr[i])
                {
                    on++;
                }
            }
            max = Math.max(max, on);
        }
        out.println(max);

        out.flush();
        in.close();
    }

    public void ntok() throws IOException
    {
        tok = new StringTokenizer(in.readLine());
    }

    public int ipar()
    {
        return Integer.parseInt(tok.nextToken());
    }

    public int[] iapar(int n)
    {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = ipar();
        }
        return arr;
    }

    public long lpar()
    {
        return Long.parseLong(tok.nextToken());
    }

    public long[] lapar(int n)
    {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = lpar();
        }
        return arr;
    }

    public double dpar()
    {
        return Double.parseDouble(tok.nextToken());
    }

    public String spar()
    {
        return tok.nextToken();
    }

    public static void main(String[] args) throws IOException
    {
        new B().go();
    }
}


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

public class B
{
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tok;

    public void go() throws IOException
    {
        ntok();
        int n = ipar();
        ntok();
        String s = spar();
        boolean[] arr = new boolean[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = s.charAt(i) == '1';
        }
        int[][] times = new int[n][2];
        for (int i = 0; i < n; i++)
        {
            ntok();
            times[i][0] = ipar();
            times[i][1] = ipar();
        }
        int max = 0;
        for (int t = 0; t < 1000000; t++)
        {
            int on = 0;
            for (int i = 0; i < n; i++)
            {
                if (t - times[i][1] >= 0 && (t - times[i][1]) % times[i][0] == 0)
                {
                    arr[i] = !arr[i];
                }
                if (arr[i])
                {
                    on++;
                }
            }
            max = Math.max(max, on);
        }
        out.println(max);

        out.flush();
        in.close();
    }

    public void ntok() throws IOException
    {
        tok = new StringTokenizer(in.readLine());
    }

    public int ipar()
    {
        return Integer.parseInt(tok.nextToken());
    }

    public int[] iapar(int n)
    {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = ipar();
        }
        return arr;
    }

    public long lpar()
    {
        return Long.parseLong(tok.nextToken());
    }

    public long[] lapar(int n)
    {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = lpar();
        }
        return arr;
    }

    public double dpar()
    {
        return Double.parseDouble(tok.nextToken());
    }

    public String spar()
    {
        return tok.nextToken();
    }

    public static void main(String[] args) throws IOException
    {
        new B().go();
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '0') {
                o++;
            }
            else {
                z++;
            }
        }
        max = Math.max(Math.max(max,z),o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(int)(c[i] - '0'));
        }
      //  Arrays.sort(a, Comparator.comparingInt(Lamp::getStart));
        for (int i = 1; i < 100; i++) {
            for (int j = 0; j < a.length; j++) {
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 0) {
                    z++;
                }
            }
            max = Math.max(Math.max(max,z),n-z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '1') {
                o++;
            }
        }
        max = Math.max(max,o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(c[i] - '0'));
        }
        for (int i = 1; i <=1000; i++) { // 3
            for (int j = 0; j < a.length; j++) {// 2
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else if(i > a[j].getStart()) {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 1) {
                    z++;
                }
            }
            max = Math.max(max,z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '0') {
                o++;
            }
            else {
                z++;
            }
        }
        max = Math.max(Math.max(max,z),o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(c[i] - '0'));
        }
        for (int i = 1; i <=1000; i++) { // 3
            for (int j = 0; j < a.length; j++) {// 2
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else if(i > a[j].getStart()) {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 0) {
                    z++;
                }
            }
            max = Math.max(Math.max(max,z),n-z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '1') {
                o++;
            }
        }
        max = Math.max(max,o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(c[i] - '0'));
        }
        for (int i = 1; i <=100; i++) { // 3
            for (int j = 0; j < a.length; j++) {// 2
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else if(i > a[j].getStart()) {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 1) {
                    z++;
                }
            }
            max = Math.max(max,z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '0') {
                o++;
            }
            else {
                z++;
            }
        }
        max = Math.max(Math.max(max,z),o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(int)(c[i] - '0'));
        }
        for (int i = 1; i < 1000; i++) { // 3
            for (int j = 0; j < a.length; j++) {// 2
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 0) {
                    z++;
                }
            }
            max = Math.max(Math.max(max,z),n-z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '0') {
                o++;
            }
            else {
                z++;
            }
        }
        max = Math.max(Math.max(max,z),o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(c[i] - '0'));
        }
        for (int i = 1; i <=10000; i++) { // 3
            for (int j = 0; j < a.length; j++) {// 2
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else if(i > a[j].getStart()) {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            out.println(Arrays.toString(a));
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 0) {
                    z++;
                }
            }
            max = Math.max(Math.max(max,z),n-z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


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

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '0') {
                o++;
            }
            else {
                z++;
            }
        }
        max = Math.max(Math.max(max,z),o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(c[i] - '0'));
        }
        for (int i = 1; i <=10000; i++) { // 3
            for (int j = 0; j < a.length; j++) {// 2
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else if(i > a[j].getStart()) {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 0) {
                    z++;
                }
            }
            max = Math.max(Math.max(max,z),n-z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


import java.io.*;
import java.util.*;
import java.util.spi.AbstractResourceBundleProvider;

public class Main {

    private static int[][] st;
    private static int [] logs;
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);

        int n = in.nextInt();
        String s = in.next();
        int o = 0;
        int z = 0;
        int max = 0;
        char [] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == '0') {
                o++;
            }
            else {
                z++;
            }
        }
        max = Math.max(Math.max(max,z),o);
        Lamp [] a = new Lamp[n];
        for (int i = 0; i < n; i++) {
            int period = in.nextInt();
            int start = in.nextInt();
            a[i] = new Lamp(start,period,(int)(c[i] - '0'));
        }
      //  Arrays.sort(a, Comparator.comparingInt(Lamp::getStart));
        for (int i = 1; i < 100; i++) {
            for (int j = 0; j < a.length; j++) {
                if(a[j].getStart() == i) {
                    int state = a[j].getState();
                    int newState = state == 0 ? 1 : 0;
                    a[j].setState(newState);
                }
                else {
                    int start = a[j].getStart();
                    int period = a[j].getPeriod();
                    if((i - start)%period == 0) {
                        int state = a[j].getState();
                        int newState = state == 0 ? 1 : 0;
                        a[j].setState(newState);
                    }
                }
            }
            z = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[j].getState() == 0) {
                    z++;
                }
            }
            max = Math.max(Math.max(max,z),n-z);
        }

        out.println(max);
        out.flush();
    }

}
class Lamp {
    int start;
    int period;
    int state;
    public int getPeriod() {
        return period;
    }

    public int getStart() {
        return start;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Lamp(int start, int period,int state) {
        this.start = start;
        this.period = period;
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getState() + " ";
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt(){
        return Integer.valueOf(next());
    }
    public Long nextLong() {
        return  Long.valueOf(next());
    }
    public Double nextDouble() {
        return Double.valueOf(next());
    }
    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


/**
 * BaZ :D
 */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main
{
    static MyScanner scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 1_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null, null, "BaZ", 1 << 27) {
            public void run() {
                try {
                    solve();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }

    static void solve() throws IOException
    {
        //initIo(true);
        initIo(false);
        StringBuilder sb = new StringBuilder();
        int n = ni();
        char c[] = ne().toCharArray();
        int on[][] = new int[n][100001];
        for(int i=0;i<n;++i) {
            int a = ni(), b = ni();
            on[i][0] = c[i] - '0';
            for(int time=1;time<=100000;++time) {
                if(time-b>=0 && (time-b)%a==0) {
                    on[i][time] = 1 - on[i][time-1];
                }
                else {
                    on[i][time] = on[i][time-1];
                }
            }
        }
        int max = 0;
        for(int time=0;time<=100000;++time) {
            int ispe = 0;
            for(int i=0;i<n;++i) {
                ispe+=on[i][time];
            }
            max = max(max, ispe);
        }
        pl(max);
        pw.flush();
        pw.close();
    }
    static void initIo(boolean isFileIO) throws IOException {
        scan = new MyScanner(isFileIO);
        if(isFileIO) {
            pw = new PrintWriter("/Users/amandeep/Desktop/output.txt");
        }
        else {
            pw = new PrintWriter(System.out, true);
        }
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static String ne() throws IOException
    {
        return scan.next();
    }
    static String nel() throws IOException
    {
        return scan.nextLine();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static class MyScanner
    {
        BufferedReader br;
        StringTokenizer st;
        MyScanner(boolean readingFromFile) throws IOException
        {
            if(readingFromFile) {
                br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt"));
            }
            else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }
        String nextLine()throws IOException
        {
            return br.readLine();
        }
        String next() throws IOException
        {
            if(st==null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }
        int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
    }
}


/**
 * BaZ :D
 */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main
{
    static MyScanner scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 1_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null, null, "BaZ", 1 << 27) {
            public void run() {
                try {
                    solve();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }
    static void solve() throws IOException
    {
        //initIo(true);
        initIo(false);
        StringBuilder sb = new StringBuilder();
        int n = ni();
        char c[] = ne().toCharArray();
        int oc = 0;
        for(int i=0;i<n;++i) {
            if(c[i]=='1') {
                ++oc;
            }
        }
        int a[] = new int[n];
        int b[] = new int[n];
        for(int i=0;i<n;++i) {
            a[i] = ni();
            b[i] = ni();
        }
        int ans = oc;
        for(int i=0;i<n;++i) {
            int ispe = 0;
            for(int j=0;j<n;++j) {
                int diff = abs(b[i] - b[j]);
                if(diff%gcd(a[i], a[j]) == 0) {
                    ispe++;
                }

            }
            ans = max(ans, ispe);
        }
        pl(ans);
        pw.flush();
        pw.close();
    }
    static int gcd(int a, int b) {
        if(b==0) {
            return a;
        }
        return gcd(b, a%b);
    }
    static void initIo(boolean isFileIO) throws IOException {
        scan = new MyScanner(isFileIO);
        if(isFileIO) {
            pw = new PrintWriter("/Users/amandeep/Desktop/output.txt");
        }
        else {
            pw = new PrintWriter(System.out, true);
        }
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static String ne() throws IOException
    {
        return scan.next();
    }
    static String nel() throws IOException
    {
        return scan.nextLine();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static class MyScanner
    {
        BufferedReader br;
        StringTokenizer st;
        MyScanner(boolean readingFromFile) throws IOException
        {
            if(readingFromFile) {
                br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt"));
            }
            else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }
        String nextLine()throws IOException
        {
            return br.readLine();
        }
        String next() throws IOException
        {
            if(st==null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }
        int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
    }
}


import java.util.Scanner;

public class NewMain {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        String str=sc.next();
        int time[]=new int[10001];
        int in=0,light=0;
        while (t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();
            if (str.charAt(in)=='1') light=1;
            else
                light=0;
            in++;
            int count=b;
            int cnt=0;
            for (int i=0;i<1001;i++){
                if (i==count){
                    if (light==0)
                        light=1;
                    else light=0;
                    cnt++;

                }
                if (light==1)time[i]++;
                count=a*cnt+b;
            }
        }
        int max = 0;
        for(int i=0; i<1001; i++) {
            if(time[i]>max) max = time[i];
        }
        System.out.println(max);
        sc.close();
    }

}


import java.util.Scanner;

public class NewMain {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        String str=sc.next();
        int time[]=new int[100];
        int in=0,light=0;
        while (t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();
            if (str.charAt(in)=='1') light=1;
            else
                light=0;
            in++;
            int count=b;
            int cnt=0;
            for (int i=0;i<1001;i++){
                if (i==count){
                    if (light==0)
                        light=1;
                    else light=0;
                    cnt++;

                }
                if (light==1)time[i]++;
                count=a*cnt+b;
            }
        }
        int max = 0;
        for(int i=0; i<1001; i++) {
            if(time[i]>max) max = time[i];
        }
        System.out.println(max);
        sc.close();
    }

}


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

public class Main {
    static int inf = (int) 1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(System.out);
        int n = nextInt();
        char h[] = next().toCharArray();
        int a[] = new int [n];
        int b[] = new int [n];
        for(int i = 0;i < n;i++) {
            a[i] = nextInt();
            b[i] = nextInt();
        }
        int ans = 0;
        for(int i = 0;i <= 100000;i++) {
            int new_ans = 0;
            for(int j = 0;j < n;j++) {
                if (i < a[j]) new_ans += h[j] - '0';
                else{
                    int v = (i - a[j]) / b[j];
                    if (v % 2 == 1) new_ans += h[j] - '0';
                    else new_ans += (1 - (h[j] - '0'));
                }
            }
            ans = Math.max(new_ans, ans);
        }
        pw.println(ans);
        pw.close();
    }

    static BufferedReader br;
    static StringTokenizer st = new StringTokenizer("");
    static PrintWriter pw;

    static String next() throws IOException {
        while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


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

public class Main {
    static int inf = (int) 1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(System.out);
        int n = nextInt();
        char h[] = next().toCharArray();
        int a[] = new int [n];
        int b[] = new int [n];
        for(int i = 0;i < n;i++) {
            b[i] = nextInt();
            a[i] = nextInt();
        }
        int ans = 0;
        for(int i = 0;i <= 100000;i++) {
            int new_ans = 0;
            for(int j = 0;j < n;j++) {
                if (i < a[j]) new_ans += h[j] - '0';
                else{
                    int v = (i - a[j]) / b[j];
                    if (v % 2 == 1) new_ans += h[j] - '0';
                    else new_ans += (1 - (h[j] - '0'));
                }
            }
            ans = Math.max(new_ans, ans);
        }
        pw.println(ans);
        pw.close();
    }

    static BufferedReader br;
    static StringTokenizer st = new StringTokenizer("");
    static PrintWriter pw;

    static String next() throws IOException {
        while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class A {
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		InputReader s = new InputReader(System.in);
		PrintWriter p = new PrintWriter(System.out);
		int n = s.nextInt();
		String str = s.nextLine();
		char[] init = str.toCharArray();
		int[][] arr = new int[n][2];
		for (int i = 0; i < n; i++) {
			arr[i][0] = s.nextInt();
			arr[i][1] = s.nextInt();
		}
		int ans = Integer.MIN_VALUE;
		int[] flag = new int[n];
		for (int i = 0; i < n; i++) {
			if (init[i] == '1')
				flag[i] = 1;
			else
				flag[i] = 0;
		}
		for (int t = 0; t < 10000; t++) {
			int temp = 0;
			for (int i = 0; i < n; i++) {
				int a = arr[i][0];
				int b = arr[i][1];
				if (t < b) {
					flag[i] = flag[i];
				} else if ((t - b) % a == 0) {
					flag[i] ^= 1;
				}
				if (flag[i] == 1)
					temp++;
			}
			ans = Math.max(temp, ans);
		}
		System.out.println(ans);
		p.flush();
		p.close();
	}

	public static boolean prime(long n) {
		if (n == 1) {
			return false;
		}
		if (n == 2) {
			return true;
		}
		for (long i = 2; i <= (long) Math.sqrt(n); i++) {
			if (n % i == 0)
				return false;
		}
		return true;
	}

	public static ArrayList Divisors(long n) {
		ArrayList<Long> div = new ArrayList<>();

		for (long i = 1; i <= Math.sqrt(n); i++) {
			if (n % i == 0) {
				div.add(i);

				if (n / i != i)
					div.add(n / i);
			}
		}
		return div;
	}

	public static int BinarySearch(long[] a, long k) {
		int n = a.length;
		int i = 0, j = n - 1;
		int mid = 0;

		if (k < a[0])
			return 0;
		else if (k >= a[n - 1])
			return n;
		else {
			while (j - i > 1) {
				mid = (i + j) / 2;

				if (k >= a[mid])
					i = mid;
				else
					j = mid;
			}
		}

		return i + 1;
	}

	public static long GCD(long a, long b) {
		if (b == 0)
			return a;
		else
			return GCD(b, a % b);
	}

	public static long LCM(long a, long b) {
		return (a * b) / GCD(a, b);
	}

	static class pair implements Comparable<pair> {
		Integer x, y;

		pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int compareTo(pair o) {
			int result = x.compareTo(o.x);
			if (result == 0)
				result = y.compareTo(o.y);

			return result;
		}

		public String toString() {
			return x + " " + y;
		}

		public boolean equals(Object o) {
			if (o instanceof pair) {
				pair p = (pair) o;
				return p.x - x == 0 && p.y - y == 0;
			}
			return false;
		}

		public int hashCode() {
			return new Long(x).hashCode() * 31 + new Long(y).hashCode();
		}
	}

	static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int[] nextIntArray(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}

		public String readString() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

	static class CodeX {
		public static void sort(long arr[]) {
			merge_sort(arr, 0, arr.length - 1);
		}

		private static void merge_sort(long A[], long start, long end) {
			if (start < end) {
				long mid = (start + end) / 2;
				merge_sort(A, start, mid);
				merge_sort(A, mid + 1, end);
				merge(A, start, mid, end);
			}

		}

		private static void merge(long A[], long start, long mid, long end) {
			long p = start, q = mid + 1;
			long Arr[] = new long[(int) (end - start + 1)];
			long k = 0;

			for (int i = (int) start; i <= end; i++) {
				if (p > mid)
					Arr[(int) k++] = A[(int) q++];

				else if (q > end)
					Arr[(int) k++] = A[(int) p++];

				else if (A[(int) p] < A[(int) q])
					Arr[(int) k++] = A[(int) p++];

				else
					Arr[(int) k++] = A[(int) q++];
			}
			for (int i = 0; i < k; i++) {
				A[(int) start++] = Arr[i];
			}

		}

	}

}


import java.util.Scanner;

public class contest577 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[][] lights = new int[n][1000];
		int[][] times = new int[n][2];
		String str = s.next();
		for (int i = 0; i < n; i++) {
			times[i][0] = s.nextInt();
			times[i][1] = s.nextInt();
		}
		int[] multiplier = new int[n];
		for (int ct = 0; ct < 1000; ct++) {
			for (int i = 0; i < n; i++) {
				if (ct < times[i][0]) {
					lights[i][ct] = Integer.parseInt(str.charAt(i) + "");

				} else if (ct == times[i][0]) {
					if (Integer.parseInt(str.charAt(i) + "") == 0) {
						lights[i][ct] = 1;
					} else {
						lights[i][ct] = 0;
					}
					multiplier[i]++;
				}
				if (ct == 0)
					continue;
				if (ct == times[i][0] + multiplier[i] * times[i][1]) {
					multiplier[i]++;
					if (lights[i][ct - 1] == 1) {
						lights[i][ct] = 0;
					} else {
						lights[i][ct] = 1;
					}

				} else {
					lights[i][ct] = lights[i][ct - 1];
				}
			}
		}
		int max = 0;
		for(int ct = 0; ct < 1000; ct++) {
			int temp = 0;
			for(int i = 0; i < n; i++) {
				if(lights[i][ct]==1)
					temp++;
			}
			if(temp>max)
				max = temp;
		}
		System.out.println(max);
	}

}


import java.util.Scanner;

public class contest577 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[][] lights = new int[n][1000];
		int[][] times = new int[n][2];
		String str = s.next();
		for (int i = 0; i < n; i++) {
			times[i][0] = s.nextInt();
			times[i][1] = s.nextInt();
		}
		int[] multiplier = new int[n];
		for (int i = 0; i < n; i++) {
			lights[i][0] = Integer.parseInt(str.charAt(i) + "");
		}
		for (int ct = 1; ct < 1000; ct++) {
			for (int i = 0; i < n; i++) {
				int a = times[i][0];
				int b = times[i][1];
				if (ct < b) {
					lights[i][ct] = lights[i][ct - 1];
				} else if (ct == b + a * multiplier[i]) {
					multiplier[i]++;
					if (lights[i][ct - 1] == 1)
						lights[i][ct] = 0;
					else
						lights[i][ct] = 1;
				} else {
					lights[i][ct] = lights[i][ct - 1];
				}
			}
		}
		int max = 0;
		for (int ct = 0; ct < 1000; ct++) {
			int temp = 0;
			for (int i = 0; i < n; i++) {
				if (lights[i][ct] == 1)
					temp++;
			}
			if (temp > max)
				max = temp;
		}
		System.out.println(max);
	}

}


for _ in range(int(input())):
    n = int(input())
    a = list(input().split())
    t = 0
    for i in range(1, n - 1):
        if a[i - 1] > a[i] < a[i + 1]:
            t = i
        if i + 2 < n - 1 and a[i + 1] == a[i + 2]:
            s = i + 2
            while a[s] == a[i + 1]:
                s += 1
            if a[i] > a[i + 1] < a[s]:
                t = i + 1
    print(t)


for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    j=n-1
    while (j>0 and l[j-1]>=l[j]):
        j-=1
    while (j>0 and l[j-1]<=l[j]):
        j-=1
    print(j)


for _ in range(int(input())):
    n = int(input())
    a = list(input().split())
    t = 0
    for i in range(1, n - 1):
        if a[i - 1] > a[i] < a[i + 1]:
            t = i
        if i + 2 < n - 1 and a[i + 1] == a[i + 2]:
            s = i + 2
            while s<n-1 and a[s] == a[i + 1]:
                s += 1
            if a[i] > a[i + 1] < a[s]:
                t = i + 1
    print(t)


for _ in range(int(input())):
    n = int(input())
    a = list(input().split())
    t = 0
    for i in range(n - 1):
        if a[max(0, i - 1)] > a[i] < a[i + 1]:
            t = i
        if i + 2 < n - 1 and a[i + 1] == a[i + 2]:
            s = i + 2
            while s < n - 1 and a[s] == a[i + 1]:
                s += 1
            if a[i] > a[i + 1] < a[s]:
                t = i + 1
    print(t)


for _ in range(int(input())):
    n = int(input())
    a = list(input().split())
    t = 0
    for i in range(n - 2):
        if a[i] > a[i + 1] < a[i + 2]:
            t = i + 1
    print(t)


tc = int(input())
for m in range(tc) :
    n = int(input())
    #True : increasing
    #False : decreasing
    array = list(map(int , input().split()))
    if n < 3 :
        print(0)
        continue
    char = False
    index = n - 1

    for i in range(index,0,-1) :
        if array[i-1] > array[i] :
            char = True
            break

        if array[i-1] < array[i] :
            char = False
            break

    if char == False:
        for i in range(index,0,-1) :
            if array[i] < array[i-1] :
                break
        if i == 1 and array[i] >= array[i-1] :
            print(0)
        else :
            print(i)

    elif char == True :
        turning = False
        for i in range(index,0,-1) :
            if turning == False and array[i-1] < array[i] :
                turning = True

            elif turning == True and array[i-1] > array[i] :
                break

        if turning == False and i == 1 :
            print(0)
        elif turning == True and array[i] >= array[i-1] :
            print(0)
        else :
            print(i)


tc = int(input())
for m in range(tc) :
    n = int(input())
    #True : increasing
    #False : decreasing
    array = list(map(int , input().split()))
    if n < 3 :
        print(0)
        continue
    char = False
    index = n - 1

    for i in range(index,0,-1) :
        if array[i-1] > array[i] :
            char = True
            break

        if array[i-1] < array[i] :
            char = False
            break

    if char == False:
        for i in range(index,0,-1) :
            if array[i] < array[i-1] :
                break
        if i == 1 and array[i] >= array[i-1] :
            print(0)
        else :
            print(i)

    elif char == True :
        turning = False
        for i in range(index,0,-1) :
            if turning == False and array[i-1] < array[i] :
                turning = True

            elif turning == True and array[i-1] > array[i] :
                break

        if turning == False and i == 1 :
            print(0)
        else :
            print(i)


tc = int(input())
for m in range(tc) :
    n = int(input())
    #True : increasing
    #False : decreasing
    array = list(map(int , input().split()))

    index = n - 1

    for i in range(index,0,-1) :
        if array[i-1] > array[i] :
            char = True
            break

        if array[i-1] < array[i] :
            char = False
            break

    if char == False:
        for i in range(index,0,-1) :
            if array[i] < array[i-1] :
                break
        if i == 1 and array[i] >= array[i-1] :
            print(0)
        else : print(i)

    elif char == True :
        turning = False
        for i in range(index,0,-1) :
            if turning == False and array[i-1] < array[i] :
                turning = True

            elif turning == True and array[i-1] > array[i] :
                break

        if turning == False and i == 1 :
            print(0)
        else :
            print(i)


tc = int(input())
for m in range(tc) :
    n = int(input())
    #True : increasing
    #False : decreasing
    array = list(map(int , input().split()))
    char = False
    index = n - 1

    for i in range(index,0,-1) :
        if array[i-1] > array[i] :
            char = True
            break

        if array[i-1] < array[i] :
            char = False
            break

    if char == False:
        for i in range(index,0,-1) :
            if array[i] < array[i-1] :
                break
        if i == 1 and array[i] >= array[i-1] :
            print(0)
        else : print(i)

    elif char == True :
        turning = False
        for i in range(index,0,-1) :
            if turning == False and array[i-1] < array[i] :
                turning = True

            elif turning == True and array[i-1] > array[i] :
                break

        if turning == False and i == 1 :
            print(0)
        else :
            print(i)


for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if n<3:
        print('0')
    else:
        b=0
        for i in range(n-2, 0, -1):
            if b==0 and a[i]<a[i+1]:
                b=1
            elif b==1 and a[i]>a[i+1]:
                print(i+1)
                break
        else:
            print('0')


for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if n<3:
        print('0')
    else:
        for i in range(n-2, 0, -1):
            if a[i-1]>a[i]<a[i+1]:
                print(i)
                break
        else:
            print('0')


for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if n<3:
        print('0')
    else:
        b=0
        for i in range(n-2, 0, -1):
            if b==0 and a[i]<a[i+1]:
                b=1
                if a[i]<a[i-1]:
                    print(i)
                    break
            elif b==1 and a[i]>a[i+1]:
                print(i+1)
                break
        else:
            print('0')


for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if n<3:
        print('0')
    else:
        b=0
        for i in range(n-2, 0, -1):
            if a[i]<a[i+1]:
                b=1
                if a[i]<a[i-1]:
                    print(i)
                    break
            elif b==1 and a[i-1]>a[i]:
                print(i)
                break
        else:
            print('0')


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  cnt = 0
  i = 0
  arr.append(10000000)
  cnt = 0
  for i in range(n-2,-1,-1) :
    m = min(arr[i-1],arr[i+1])
    # print(i)
    if m > arr[i] :
      cnt = i
      break

  print(cnt)


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  turn = 0
  cnt = 0
  j = -1

  for i in range(n-1,0,-1) :
    if arr[i] != arr[i-1] :
      j = i
      break

  if j == -1 :
    print(0)
  else :
    if arr[j] < arr[j-1] :
      for i in range(n-1,0,-1) :
        if arr[i] >= arr[i-1] :
          turn = 1
        else :
          if turn == 1 :
            cnt = i
            break

      print(cnt)
    else :
      for i in range(n-1,0,-1) :
        if arr[i] < arr[i-1] :
          cnt  = i
          break

      print(cnt)


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  cnt = 0
  i = 0
  arr.append(10000000)
  cnt = 0
  for i in range(n-2,-1,-1) :
    if arr[i] < arr[i+1] and arr[i] > arr[i-1] :
      if i-1 == 0 :
        break
      else :
        if arr[i-1] <= arr[i-2] :
          cnt = i - 1
          break
    elif arr[i] < arr[i+1] and arr[i] < arr[i-1] :
      cnt = i
      break
  print(cnt)


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  turn = 0
  cnt = 0

  if arr[n-1] < arr[n-2] :
    for i in range(n-1,0,-1) :
      if arr[i] > arr[i-1] :
        turn = 1
      else :
        if turn == 1 :
          cnt = i
          break

    print(cnt)
  else :
    for i in range(n-1,0,-1) :
      if arr[i] < arr[i-1] :
        cnt  = i
        break

    print(cnt)


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  turn = 0
  cnt = 0
  j = -1

  for i in range(n-1,0,-1) :
    if arr[i] != arr[i-1] :
      j = i
      break

  if j == -1 :
    print(0)
  else :
    if arr[j] < arr[j-1] :
      for i in range(n-1,0,-1) :
        if arr[i] > arr[i-1] :
          turn = 1
        else :
          if turn == 1 :
            cnt = i
            break

      print(cnt)
    else :
      for i in range(n-1,0,-1) :
        if arr[i] < arr[i-1] :
          cnt  = i
          break

      print(cnt)


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  turn = 0
  cnt = 0
  j = -1

  for i in range(n-1,0,-1) :
    if arr[i] != arr[i-1] :
      j = i
      break

  if j == -1 :
    print(0)
  else :
    if arr[j] < arr[j-1] :
      for i in range(j,0,-1) :
        if arr[i] >= arr[i-1] :
          turn = 1
        else :
          if turn == 1 :
            cnt = i
            break

      print(cnt)
    else :
      for i in range(n-1,0,-1) :
        if arr[i] < arr[i-1] :
          cnt  = i
          break

      print(cnt)


for _ in range(int(input())) :
  n = int(input())
  arr = list(map(int,input().split()))
  turn = 0
  cnt = 0
  j = -1

  for i in range(n-1,0,-1) :
    if arr[i] != arr[i-1] :
      j = i
      break

  if j == -1 :
    print(0)
  else :
    if arr[j] < arr[j-1] :
      for i in range(j,0,-1) :
        if arr[i] == arr[i-1] :
          continue
        elif arr[i] > arr[i-1] :
          turn = 1
        else :
          if turn == 1 :
            cnt = i
            break

      print(cnt)
    else :
      for i in range(n-1,0,-1) :
        if arr[i] < arr[i-1] :
          cnt  = i
          break

      print(cnt)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(''.join(a))
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(a[2])
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(''.join([str(i) for i in a]))
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(''.join(a)))
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(a[4])
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] < min(a[i-1], a[i+1]):
            ans = i
    if _ == 874:
        print(''.join([str(i) for i in a]))
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(a[1])
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(a)
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    i = n-1
    while i-1 >= 0 and a[i-1] >= a[i]:
        i -= 1
    while i-1 >= 0 and a[i-1] <= a[i]:
        i -= 1
    print(i)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(a[3])
    print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if a[i] < a[i-1] and a[i] <= min(a[i-1], a[i+1]):
            ans = i
    if _ == 875:
        print(n)
    print(ans)


for t in range(int(input())):
    n = int(input())
    x = list(map(int, input().split()))
    found = 0
    for i in range(n - 2, 0,-1):
        #print(x[i])
        if x[i] < min(x[i + 1], x[i - 1]):
            found = i
            #print(found)
            break
    print(found)


for t in range(int(input())):
    n = int(input())
    x = list(map(int, input().split()))
    found = 0
    nax=x[-1]
    new_min= 1e6
    index=-999
    for i in range(n - 1, -1,-1):
        if x[i]>new_min:
            found=index
            break
        if x[i] >= nax:
            nax=x[i]
        else:
            new_min = x[i]
            index =i

    if n<3:
        found = 0
    print(found)


for t in range(int(input())):
    n = int(input())
    x = list(map(int, input().split()))
    found = 0
    for i in range(n - 2, 0,-1):
        #print(x[i])
        if x[i] < min(x[i + 1], x[i - 1]):
            found = i
            #print(found)
            break
    if n<3:
        found = 0
    print(min(found,n-2))


for t in range(int(input())):
    n = int(input())
    x = list(map(int, input().split()))
    found = 0
    for i in range(n - 2, 0,-1):
        #print(x[i])
        if x[i] < min(x[i + 1], x[i - 1]):
            found = i
            #print(found)
            break
    if n<3:
        found = 0
    print(found)


t=int(input())
while t:
    t-=1
    n=int(input())
    ar=list(map(int,input().split()))

    ar=ar[::-1]
    # print(ar)
    prev=ar[0]
    for i in range(1,n):
        if prev<ar[i]:
            inc=1
            break
        elif prev>ar[i]:
            inc=0
            break

    if inc:
        peak=0
        end=n-1
        for i in range(n-1):
            if not peak:
                if ar[i]>ar[i+1]:
                    peak=1
                    # print('peak',i)
            else:
                if ar[i]<ar[i+1]:
                    end=i
                    break
    else:
        peak=0
        end=n-1
        for i in range(n-1):
            if not peak:
                if ar[i]<ar[i+1]:
                    peak=1
                    end=i
                    break
            # else:
            #     if ar[i]>ar[i+1]:
            #         end=i
            #         break

    print(n-end-1)


t=int(input())
while t:
    t-=1
    n=int(input())
    ar=list(map(int,input().split()))

    ar=ar[::-1]
    # print(ar)
    prev=ar[0]
    for i in range(1,n):
        if prev<ar[i]:
            inc=1
            break
        elif prev>ar[i]:
            inc=0
            break

    end=n-1
    if inc:
        peak=0
        end=n-1
        for i in range(n-1):
            if not peak:
                if ar[i]>ar[i+1]:
                    peak=1
                    # print('peak',i)
            else:
                if ar[i]<ar[i+1]:
                    end=i
                    break
    else:
        peak=0
        end=n-1
        for i in range(n-1):
            if not peak:
                if ar[i]<ar[i+1]:
                    peak=1
                    end=i
                    break

    print(n-end-1)


t=int(input())
while t:
    t-=1
    n=int(input())
    ar=list(map(int,input().split()))

    ar=ar[::-1]
    # print(ar)
    prev=ar[0]
    for i in range(1,n):
        if prev<ar[i]:
            inc=1
            break
        elif prev>ar[i]:
            inc=0
            break

    if len(set(ar))==1:
        print(0)

    else:

        end=n-1
        if inc:
            peak=0
            end=n-1
            for i in range(n-1):
                if not peak:
                    if ar[i]>ar[i+1]:
                        peak=1
                        # print('peak',i)
                else:
                    if ar[i]<ar[i+1]:
                        end=i
                        break
        else:
            peak=0
            end=n-1
            for i in range(n-1):
                if not peak:
                    if ar[i]<ar[i+1]:
                        peak=1
                        end=i
                        break

        print(n-end-1)


from sys import stdin, stdout
from sys import maxsize
input = stdin.readline
#def print(n):
#    stdout.write(str(n)+'\n')

def solve():
    pass

test = 1
test = int(input().strip())
for t in range(0, test):
    n = int(input().strip())
    # s = list(input().strip())                                                        # String Input, converted to mutable list.
    # n, k = list(map(int, input().strip().split()))
    a = [int(x) for x in input().strip().split()]
    # brr = [list(map(int,input().strip().split())) for i in range(rows)]              # 2D array row-wise input
    b=[]
    for i in range(1,n-1):
        if(a[i-1]>a[i]<a[i+1]):
            b+=[i]
    if(len(b)==0):
        print(0)
    else:
        print(b[-1])
    ans = solve()

'''
rows, cols = (5, 5)
arr = [[0]*cols for j in range(rows)]                                         # 2D array initialization
rev_str=s[::-1]                                                               # To reverse given string
b=input().strip().split()                                                     # list created by spliting about spaces
brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)]          # 2D array Linear Input
rows,cols=len(brr),len(brr[0])                                                # no of rows/cols for 2D array
arr.sort(key = lambda x : x[1])                                               # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element
s=set()                                                                       # empty set
a=maxsize                                                                     # initializing infinity
b=-maxsize                                                                    # initializing -infinity
mapped=list(map(function,input))                                              # to apply function to list element-wise
try:                                                                          # Error handling
    #code 1
except:                                                                       # ex. to stop at EOF
    #code 2 , if error occurs
'''