Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

Input

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Output

Print a single integer — the minimum total time needed to turn off all the lights.

Examples

Input

2 2
0010
0100


Output

5


Input

3 4
001000
000010
000010


Output

12


Input

4 3
01110
01110
01110
01110


Output

18

Note

In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.

In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.

In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
	        			/*
	        		     * Author- Priyam Vora
	        		     * BTech 2nd Year DAIICT
	        		     */
	     
	     
	        		    import java.awt.Checkbox;
	import java.awt.Point;
	import java.io.*;
	        		    import java.math.*;
	        		    import java.util.*;
	import java.util.Map.Entry;
	
	import javax.print.attribute.SetOfIntegerSyntax;
	
	     
	        		    public class Main{
	        		  	private static InputStream stream;
	        		    	private static byte[] buf = new byte[1024];
	        		    	private static int curChar;
	        		    	private static int numChars;
	        		    	private static SpaceCharFilter filter;
	        		    	private static PrintWriter pw;
	        		    	private static long count = 0,mod=1000000007;
	        		    //	private static TreeSet<Integer> ts=new TreeSet[200000];
	        		    	public final static int INF = (int) 1E9;
	
	        		        
	        		        public static void main(String[] args) {
	        		        	InputReader(System.in);
	        		    		pw = new PrintWriter(System.out); 
	        		            new Thread(null ,new Runnable(){
	        		               public void run(){
	        		                   try{
	        		                       solve();
	        		                       pw.close();
	        		                   } catch(Exception e){
	        		                       e.printStackTrace();
	        		                   }
	        		               }
	        		           },"1",1<<26).start();
	        		       }
	        		        public static long pow(long n, long p,long mod) {
	        					if(p==0)
	        						return 1;
	        					if(p==1)
	        						return n%mod;
	        					if(p%2==0){
	        						long temp=pow(n, p/2,mod);
	        					return (temp*temp)%mod;
	        					}else{
	        						 	long temp=pow(n,p/2,mod);
	        						 	temp=(temp*temp)%mod;
	        						 	return(temp*n)%mod;
	        						 	
	        					}
	        				}
	        		        public static long pow(long n, long p) {
	        					if(p==0)
	        						return 1;
	        					if(p==1)
	        						return n;
	        					if(p%2==0){
	        						long temp=pow(n, p/2);
	        					return (temp*temp);
	        					}else{
	        						 	long temp=pow(n,p/2);
	        						 	temp=(temp*temp);
	        						 	return(temp*n);
	        						 	
	        					}
	        				}
	        		        public static void Merge(long a[],int p,int r){
	        		            if(p<r){
	        		                int q = (p+r)/2;
	        		                Merge(a,p,q);
	        		                Merge(a,q+1,r);
	        		                Merge_Array(a,p,q,r);
	        		            }
	        		        }
	        		        public static void Merge_Array(long a[],int p,int q,int r){
	        		           long b[] = new long[q-p+1];
	        		            long c[] = new long[r-q];
	        		            for(int i=0;i<b.length;i++)
	        		                b[i] = a[p+i];
	        		            for(int i=0;i<c.length;i++)
	        		                c[i] = a[q+i+1];
	        		            int i = 0,j = 0;
	        		            for(int k=p;k<=r;k++){
	        		                if(i==b.length){
	        		                    a[k] = c[j];
	        		                    j++;
	        		                }
	        		                else if(j==c.length){
	        		                    a[k] = b[i];
	        		                    i++;
	        		                }
	        		                else if(b[i]<c[j]){
	        		                    a[k] = b[i];
	        		                    i++;
	        		                }
	        		                else{
	        		                    a[k] = c[j];
	        		                    j++;
	        		                }
	        		            }
	        		        }
	        		    	public static long gcd(long x, long y) {
	        		    		if (x == 0)
	        		    			return y;
	        		    		else
	        		    			return gcd( y % x,x);
	        		    	}	public static boolean isPrime(int n) {
	        		    	// Corner cases
				    		if (n <= 1)
				    			return false;
				    		if (n <= 3)
				    			return true;
				     
				    		// This is checked so that we can skip 
				    		// middle five numbers in below loop
				    		if (n % 2 == 0 || n % 3 == 0)
				    			return false;
				     
				    		for (int i = 5; i * i <= n; i = i + 6)
				    			if (n % i == 0 || n % (i + 2) == 0)
				    				return false;
				     
				    		return true;
				    	}
	        		        static LinkedList<Integer> adj[];
	        		        static boolean Visited[];
	        		        static HashSet<Integer> exc;
	        		        static long oddsum[]=new long[1000001];
	        		        static int co=0,ans=0;
	        		       
	        		        static int n,m;
	        		        static String s[];
	        		        static int ind;
	        		        public static void solve() {
	        		  

	        		        	 int n=nextInt();
	        		    		 int m=nextInt();
	        		    		 m+=2;
	        		    		 int cnt=0;
	        		    		 char a[][]=new char[n+1][m+1];
	        		    		 for(int i=01;i<=n;i++)
	        		    		 {
	        		    			 String s=nextLine();
	        		    			 for(int j=1;j<=m;j++){
	        		    				 a[i][j]=s.charAt(j-1);
	        		    				 if(a[i][j]=='1')cnt++;
	        		    			 }
	        		    		 }
	        		    		 int dp[][]=new int[n+1][2];
	        		    		 int last=-1;
	        		    		 int c=0;
	        		    		 for(int i=1;i<=m;i++)
	        		    		 {
	        		    			 if(a[n][i]=='1')
	        		    			 {
	        		    				 c++;
	        		    				 last=i;
	        		    			 }
	        		    		 }
	        		    		 if(last==-1)
	        		    		 {
	        		    			 dp[n][0]=0;
	        		    			 dp[n][1]=(m-1);
	        		    		 }
	        		    		 else
	        		    		 {
	        		    		 dp[n][0]=2*(last-1);
	        		    		 dp[n][1]=(m-1);
	        		    	 }
	        		         //   int c=0;
	        		    		 int index=-1;
	        		    		 if(c==cnt)
	        		    		 {
	        		    			 index=n;
	        		    		 }
	        		    	
	        		    		 for(int i=n-1;i>=1;i--)
	        		    		 {
	        		    			  last=-1;
	        		    			  int first=-1;
	        		    			 for(int j=1;j<=m;j++)
	        		    			 {
	        		    				 
	        		    				 if(a[i][j]=='1')
	        		    				 {
	        		    					 c++;
	        		    					 if(first==-1)
	        		    					 {
	        		    						 first=j;
	        		    					 }
	        		    					 last=j;
	        		    				 }
	        		    			 }
	        		    			 if(last==-1)
	        		    			 {
	        		    				 if(c==cnt)
	        		    				 {
	        		    					 if(index==-1)
	        		    					 {
	        		    						 index=i;
	        		    					 }
	        		    					 dp[i][0]=dp[i+1][0];
	        		    					 dp[i][1]=dp[i+1][1];
	        		    					 continue;
	        		    				 }
	        		    				 dp[i][0]=dp[i+1][0]+1;
	        		    				 dp[i][1]=dp[i+1][1]+1;
	        		    				 continue;
	        		    			 }
	        		    			 if(c==cnt)
	        		    			 {
	        		    				 if(index==-1)
	        		    				 {
	        		    					 index=i;
	        		    				 }
	        		    			 }
	        		    			 int one=(2)*(last-1)+dp[i+1][0]+1;
	        		    			 int two=(m+dp[i+1][1]);
	        		    			 dp[i][0]=Math.min(one,two);
	        		    			 int three=(m+dp[i+1][0]);
	        		    			 int four=1+dp[i+1][1]+(2)*(m-first);
	        		    			 dp[i][1]=Math.min(three, four);
	        		    		 } 
	        		    		 last=-1;
	        		    		 int first=-1;
	        		    		 for(int j=1;j<=m;j++)
	        					 {
	        						 
	        						 if(a[index][j]=='1')
	        						 {
	        							 if(first==-1)
	        							 {
	        								 first=j;
	        							 }
	        							 last=j;
	        						 }
	        					 }
	        		    		 if(index==n)
	        		    		 {
	        		    			 int ans=-1;
	        		    			 for(int j=1;j<=m;j++)
	        		    			 {
	        		    				// System.out.print(a[n][j]);
	        		    				 if(a[n][j]=='1')
	        		    				 {
	        		    					 ans=j;
	        		    				 }
	        		    			 }
	        		    			 if(ans==-1)
	        		    			 {
	        		    				 System.out.println(0);
	        		    				 return;
	        		    			 }
	        		    			 System.out.println(ans-1);
	        		    			 return;
	        		    		 }
	        		    		 int diff=dp[index][0]-first+1;
	        		    		 int dist=(m-last);
	        		    		 diff=Math.min(dp[index][1]-dist,diff);
	        		    		 System.out.println(diff);
	        		        }
	        		     
	        		       
	        		        public static String reverseString(String s) {
	        					StringBuilder sb = new StringBuilder(s);
	        					sb.reverse();
	        					return (sb.toString());
	        				}
	        		        
	        		        
	        		        private static void BFS(int sou,int dest){
	        		        	Queue<Integer> q=new LinkedList<Integer>();
	        		        	q.add(sou);
	        		        	Visited[sou]=true;
	        		        	while(!q.isEmpty()){
	        		        		int top=q.poll();
	        		        		
	        		        		for(int i:adj[top]){
	        		        			//pw.println(i+" "+top);
	        		        			if(!Visited[i])
	        		        			{
	        		        				
	        		        				q.add(i);
	        		        			}
	        		        			
	        		        			Visited[i]=true;
	        		        			if(i==dest){
	        		        				pw.println("Yes");
	        		        			return;	
	        		        			}
	        		        		}
	        		        	}
	        		        	pw.println("No");
	        		        	
	        		        	
	        		        }
	        		        
	        		        private static long ncr(int n,int k){
	    				    	if (k < 0 || k > n) return 0;
	    				        if (n-k < k) k = n-k;
	    			
	    				        BigInteger x = BigInteger.ONE;
	    				        for (int i = 1; i <= k; i++) {
	    				            x = x.multiply(new BigInteger(""+(n-i+1)));
	    				            x = x.divide(new BigInteger(""+i));
	    				        }
	    			
	    				return x.longValue();
	    				    }
	        		       private  static long fact(long count){
	        		        	long ans=1;
	        		        	for(int i=1;i<=count;i++){
	        		        		ans*=i;
	        		        	}
	        		        	return ans;
	        		        }
	        		        
	        		        
	        		        static int state=1;
	        		        static long no_exc=0,no_vert=0;
	        		      static Stack<Integer> st;
	        		      static HashSet<Integer> inset;
	        		        private static void topo(int curr){
	        		        	
	        		        	Visited[curr]=true;
	        		        	inset.add(curr);
	        		        	for(int x:adj[curr]){
	        		        		if(adj[x].contains(curr) || inset.contains(x)){
	        		        			state=0;
	        		        			return;
	        		        		}
	        		        		if(state==0)
	        		        			return;
	        		        		
	        		        	}
	        		        	st.push(curr);
	        		        	
	        		        	inset.remove(curr);
	        		        }
	        		        static HashSet<Integer> hs;
	        		       
	        		     	private static void buildgraph(int n){
	        		    		adj=new LinkedList[n+1];
	        		    		Visited=new boolean[n+1];
	        		    		
	        		    		for(int i=0;i<=n;i++){
	        		    			adj[i]=new LinkedList<Integer>();
	        		    		
	        		    		}
	     
	     
	        		    	}
	        		     	
	        		    	// To Get Input
	        		    	// Some Buffer Methods
	     
	        		     	public static void sort(long a[]){
	        		     		Merge(a, 0, a.length-1);
	        		     	}
	        		    	public static void InputReader(InputStream stream1) {
	        		    		stream = stream1;
	        		    	}
	     
	        		    	private static boolean isWhitespace(int c) {
	        		    		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	        		    	}
	     
	        		    	private static boolean isEndOfLine(int c) {
	        		    		return c == '\n' || c == '\r' || c == -1;
	        		    	}
	     
	        		    	private static 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++];
	        		    	}
	     
	        		    	private static 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;
	        		    	}
	     
	        		    	private static 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;
	        		    	}
	     
	        		    	private static String nextToken() {
	        		    		int c = read();
	        		    		while (isSpaceChar(c))
	        		    			c = read();
	        		    		StringBuilder res = new StringBuilder();
	        		    		do {
	        		    			res.appendCodePoint(c);
	        		    			c = read();
	        		    		} while (!isSpaceChar(c));
	        		    		return res.toString();
	        		    	}
	     
	        		    	private static String nextLine() {
	        		    		int c = read();
	        		    		while (isSpaceChar(c))
	        		    			c = read();
	        		    		StringBuilder res = new StringBuilder();
	        		    		do {
	        		    			res.appendCodePoint(c);
	        		    			c = read();
	        		    		} while (!isEndOfLine(c));
	        		    		return res.toString();
	        		    	}
	     
	        		    	private static int[] nextIntArray(int n) {
	        		    		int[] arr = new int[n];
	        		    		for (int i = 0; i < n; i++) {
	        		    			arr[i] = nextInt();
	        		    		}
	        		    		return arr;
	        		    	}
	     
	        		    	private static long[][] next2dArray(int n, int m) {
	        		    		long[][] arr = new long[n][m];
	        		    		for (int i = 0; i < n; i++) {
	        		    			for (int j = 0; j < m; j++) {
	        		    				arr[i][j] = nextLong();
	        		    			}
	        		    		}
	        		    		return arr;
	        		    	}
	        		    	private static char[][] nextCharArray(int n,int m){
	        		    		char [][]c=new char[n][m];
	        		    		for(int i=0;i<n;i++){
	        		    			String s=nextLine();
	        		    			for(int j=0;j<s.length();j++){
	        		    				c[i][j]=s.charAt(j);
	        		    			}
	        		    		}
	        		    		return c;
	        		    	}
	     
	        		    	private static long[] nextLongArray(int n) {
	        		    		long[] arr = new long[n];
	        		    		for (int i = 0; i < n; i++) {
	        		    			arr[i] = nextLong();
	        		    		}
	        		    		return arr;
	        		    	}
	     
	        		    	private static void pArray(int[] arr) {
	        		    		for (int i = 0; i < arr.length; i++) {
	        		    			pw.print(arr[i] + " ");
	        		    		}
	        		    		pw.println();
	        		    		return;
	        		    	}
	     
	        		    	private static void pArray(long[] arr) {
	        		    		for (int i = 0; i < arr.length; i++) {
	        		    			pw.print(arr[i] + " ");
	        		    		}
	        		    		pw.println();
	        		    		return;
	        		    	}
	     
	        		    	private static void pArray(boolean[] arr) {
	        		    		for (int i = 0; i < arr.length; i++) {
	        		    			pw.print(arr[i] + " ");
	        		    		}
	        		    		pw.println();
	        		    		return;
	        		    	}
	     
	        		    	private static boolean isSpaceChar(int c) {
	        		    		if (filter != null)
	        		    			return filter.isSpaceChar(c);
	        		    		return isWhitespace(c);
	        		    	}
	     
	        		    	private interface SpaceCharFilter {
	        		    		public boolean isSpaceChar(int ch);
	        		    	}
	     
	     
	    }
	        		    
	        		           		    class Node{
	        		    	int to;
	        		    	long dist;
	        		    	Node(int to,long dist){
	        		    		this.to=to;
	        		    		this.dist=dist;
	        		    	}
	        		    	
	        		    }
	        		    class Pair implements Comparable<Pair>{
	        				
	    					
	    					int val,side;
	    					Pair(int val,int side){
	    					
	    						this.val=val;
	    						this.side=side;
	    					
	    					}
	    					@Override
	    					public int compareTo(Pair o) {
	    				
	    					/*	if(((double)a/(double)b)!=((double)o.a/(double)o.b))
	    						return (int)(((double)o.a/(double)o.b)-((double)a/(double)b));
	    						else*/
	    							
	    						//return (int)(o.size-size);
	    						return val-o.val;
	    					}
	    					public int hashCode() {
	    							//int hu = (int) (x ^ (x >>> 32));
	    							//int hv = (int) (y ^ (y >>> 32));
	    							//int hw = (int) (mass ^ (mass >>> 32));
	    							//return 31 * hu + hv ;
	    						return 0;
	    						}
	    						public boolean equals(Object o) {
	    							Pair other = (Pair) o;
	    						//	return x == other.x && y == other.y;
	    						return false;
	    						}
	    				}  class Dsu{
		    		    	private int rank[], parent[] ,n;
		    		    	private static int[] parent1;
		    		    	Dsu(int size){
		    		    		this.n=size+1;
		    		    		rank=new int[n];
		    		    		//parent=new int[n];
		    		    		parent=new int[n];
		    		    	makeSet();
		    		
		    		    	}
		    		    	
		    		    	void makeSet(){
		    		    		for(int i=0;i<n;i++){
		    		    			parent[i]=i;
		    		    		}
		    		    	}
		    		    	
		    		    	int find(int x){
		    		    		if(parent[x]!=x){
		    		    			
		    		    			parent[x]=find(parent[x]);
		    		    		}
		    		    		return parent[x];
		    		    	}
		    		    
		    		    	
		    		    	boolean union(int x,int y){
		    		    		int xRoot=find(x);
		    		    		int yRoot=find(y);
		    		    		
		    		    		if(xRoot==yRoot)
		    		    			return false;
		    		    		if(rank[xRoot]<rank[yRoot]){
		    		    		parent[xRoot]=yRoot;	
		    		    		}else if(rank[yRoot]<rank[xRoot]){
		    		    			parent[yRoot]=xRoot;
		    		    		}else{
		    		    			parent[yRoot]=xRoot;
		    		    			rank[xRoot]++;
		    		    		}
		    		    		return true;
		    		    	}
	
		    		     
		    		    }