// ACSL Contest 4 by Mohit Agrawal

// W Lafayette HS, W Lafayette, IN

// INT-5 Division

// School Code 8019

 

 

import java.util.*;

 

public class Contest4 {

 

            static class Bird {

 

                        boolean moved;

                       

                        public Bird() {

                       

                                    moved = false;

                                   

                        }

 

            }

 

            public static void main(String[] argv) {

 

                        Scanner input=new Scanner(System.in);

                        for(int i=0; i<5; i++) {

 

                                    String str=input.next();

                                    flock_sim(str);

 

                        }

 

                        input.close();

 

 

            }

 

            private static void flock_sim(String str) {

 

                        StringTokenizer toke=new StringTokenizer(str, ",");

                        int count=Integer.parseInt(toke.nextToken());

 

                        Bird[] flock=new Bird[10];

 

                        for(int i=0; i<count; i++) {

 

                                    int index=Integer.parseInt(toke.nextToken());

                                    flock[index-1]=new Bird();

 

                        }

 

                        int stages=Integer.parseInt(toke.nextToken()) - 1;

 

                                    /*

                                    for (int j = 0; j < 10; j++)

                                    {

                                                if (flock[j] == null)

                                                {

                                                            System.out.print(0 + " ");

                                                }

                                                else

                                                {

                                                            System.out.print(1 + " ");

                                                }

                                               

                                    }

 

                                    System.out.println();

                                    */

 

                        for(int i=0; i<stages; i++) {

 

 

                                    if(flock[0]==null&&flock[9]!=null) {

                                   

                                                flock[0]=flock[9];

                                                flock[0].moved=true;

                                                flock[9]=null;

 

                                    }

 

                                    for(int j=1; j<10; j++) {

 

                                                if(flock[j]==null&&flock[j-1]!=null&&flock[j-1].moved==false) {

 

                                                            flock[j]=flock[j-1];

                                                            flock[j].moved=true;

                                                            flock[j-1]=null;

 

                                                }

 

                                    }

 

                                    for (int j = 0; j < 10; j++)

                                    {

                                                if (flock[j] != null) flock[j].moved = false;

                                    }

 

                                    /*

                                    for (int j = 0; j < 10; j++)

                                    {

                                                if (flock[j] == null)

                                                {

                                                            System.out.print(0 + " ");

                                                }

                                                else

                                                {

                                                            System.out.print(1 + " ");

                                                }

                                               

                                    }

 

                                    System.out.println();

                                    */

 

                        }

 

                        int current=0;

                        int max=0;

                       

                        for(int i=0; i<10; i++) {

           

                                    if(flock[i]!=null) {

 

                                                current++;

 

                                                if(current>max)

                                                            max=current;

 

                                    } else

                                                current=0;

 

                        }

 

                        System.out.println(max);

 

            }

 

}