//Prashant Tyagi
using namespace std;
#include <bits/stdc++.h>
#define TRACE
#ifdef TRACE
#define TR(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define TR(...)
#endif
typedef  long long                     LL;
typedef  vector < int >                VI;
typedef  pair< int, int >              II;
typedef  vector < II >                 VII;
#define SI(x)                          scanf ("%d", &x)
#define SLL(x)                         scanf ("%lld", &x)
#define PI(x)                          printf ("%d\n", x)
#define PLL(x)                         printf ("%lld\n", x)
#define MOD                            98765431
#define PB                             push_back
#define MP                             make_pair
#define ALL(v)                         v.begin(), v.end()
#define VIF(x,V)                       for(int x:V)
#define FILL(a,b)                      memset(a,b,sizeof(a))
#define REP(i,j,n)                     for(int i=j;i<n;i++)
#define FLD(i,j,n)                     for(int i=n-1;i>=j;i--)
#define F                              first
#define S                              second
#define fast_io                        ios_base::sync_with_stdio(false);cin.tie(NULL)
const int M=1e5+1;
const int N=5e3+5;
int A[N];
int B[N];
unordered_map<int,int> S;
unordered_map<int,int> M1;
bool prime[M];

void seive()
{
	REP(i,2,M)prime[i]=true;
	for(int i=2;i*i<M;i++){
		if(prime[i]){
			for(int j=2*i;j<M;j+=i){
				prime[j]=false;
			}
		}
	}
	REP(i,2,M){
		if(prime[i])M1[i];
	}
}

int main ()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	seive();
	int n,m;SI(n);SI(m);
	REP(i,1,n+1){
		SI(A[i]);
	}
	int gd=A[1];
	REP(i,1,n+1){
		gd=__gcd(gd,A[i]);
		B[i]=gd;
	}
	for(int i=1;i<=n;i++){
		int num=A[i];
		for(auto itr=M1.begin();itr!=M1.end();++itr){
			while(num%itr->F==0)num/=itr->F;
		}
		if(num>1)M1[num];
		num=B[i];
		for(auto itr=M1.begin();itr!=M1.end();++itr){
			while(num%itr->F==0)num/=itr->F;
		}
		if(num>1)M1[num];
	}
	REP(i,1,m+1){
		int a ;SI(a);
		S[a];
	}
	int ans=0;
	int p,q;
	p=0;
	q=0;
	for(int i=1;i<=n;i++){
		for(auto itr=M1.begin();itr!=M1.end();++itr){
			int num=0;
			while(A[i]%itr->F==0)A[i]/=itr->F,num++;
			if(S.find(itr->F)!=S.end())ans-=num;
			else ans+=num;
		}
	}
	for(int i=n;i>=1;i--){
		int p1,q1;
		p1=q1=0;
		for(auto itr=M1.begin();itr!=M1.end();++itr){
			int num=0;
			while(B[i]%itr->F==0)B[i]/=itr->F,num++;
			if(S.find(itr->F)!=S.end())q1+=num;
			else p1+=num;
		}
		p1-=p;
		q1-=q;
		if(p1<q1){
			ans+=(q1-p1)*i;
			p+=p1;
			q+=q1;
		}
	}
	PI(ans);
	return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

long long z, n, m, l, s, x, y, maxx, pr[maxn], a[maxn], b[maxn];
unordered_map <int, bool> bad ;

int f(int x){
    int z = sqrt(x) + 1 ;
    int b = 0, g = 0, t = 0;
    for (int i = 2; i <= z; i++){
	if (pr[i]){
	    t = 0 ;
	    while (x % i == 0)
		x = x / i, t++ ;
	    if (bad[i])
		b = b + t ;
	    else
		g = g + t ;
	}
    }
    if (x > 1){
	if (bad[x])
	    b++ ;
	else
	    g++ ;
    }
    return b - g ;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    fill(pr, pr + maxn, 1) ;
    pr[1] = 0, pr[2] = 1 ;
    for (int i = 2; i <= 100000; i++){
	if (pr[i]){
	    l = 2;
	    while(l * i <= 100000)
		pr[l * i]  = 0, l++ ;
	}
    }
    l = 0 ;
    cin >> n >> m ;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < m; i++)
	cin >> b[i], bad[b[i]] = 1;
    for (int i = n - 1; i >= 0; i--){
	l = a[i] ;
	for (int j = i - 1; j >= 0; j--)
	    l = __gcd(a[j], l) ;
	if (f(l) > 0){
	    for (int j = i; j >= 0; j--)
		a[j] = a[j] / l ;
	}
    }
    l = 0 ;
    for (int i = 0; i < n; i++)
	l = l - f(a[i]) ;
    cout << l ;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

long long z, n, m, l, s, x, y, maxx, pr[maxn], a[maxn], b[maxn];
unordered_map <int, bool> bad ;
vector <int> p ;

int f(int x){
    int b = 0, g = 0, t = 0;
    for (int i = 0; i < z; i++){
	if (p[i] > x)
	    break ;
	t = 0 ;
	while (x % p[i] == 0)
	    x = x / p[i], t++ ;
	if (bad[p[i]])
	    b = b + t ;
	else
	    g = g + t ;
    }
    if (x > 1){
	if (bad[x])
	    b++ ;
	else
	    g++ ;
    }
    return b - g ;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    fill(pr, pr + maxn, 1) ;
    pr[1] = 0, pr[2] = 1 ;
    for (int i = 2; i <= 100000; i++){
	if (pr[i]){
	    p.pb(i) ;
	    l = 2;
	    while(l * i <= 100000)
		pr[l * i]  = 0, l++ ;
	}
    }
    l = 0 ;
    cin >> n >> m ;
    z = p.size() ;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < m; i++)
	cin >> b[i], bad[b[i]] = 1;
    for (int i = n - 1; i >= 0; i--){
	l = a[i] ;
	for (int j = i - 1; j >= 0; j--)
	    l = __gcd(a[j], l) ;
	if (f(l) > 0){
	    for (int j = i; j >= 0; j--)
		a[j] = a[j] / l ;
	}
    }
    l = 0 ;
    for (int i = 0; i < n; i++)
	l = l - f(a[i]) ;
    cout << l ;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

int z, n, m, l, s, x, y, maxx, pr[maxn], a[maxn], b[maxn];
unordered_map <int, bool> bad ;
vector <int> p ;

int f(int x){
    int b = 0, g = 0, t = 0;
    for (int i = 0; i < z; i++){
	if (p[i] > x)
	    break ;
	t = 0 ;
	while (x % p[i] == 0)
	    x = x / p[i], t++ ;
	if (bad[p[i]])
	    b = b + t ;
	else
	    g = g + t ;
    }
    if (x > 1){
	if (bad[x])
	    b++ ;
	else
	    g++ ;
    }
    return b - g ;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    fill(pr, pr + maxn, 1) ;
    pr[1] = 0, pr[2] = 1 ;
    for (int i = 2; i <= 100000; i++){
	if (pr[i]){
	    p.pb(i) ;
	    l = 2;
	    while(l * i <= 100000)
		pr[l * i]  = 0, l++ ;
	}
    }
    l = 0 ;
    cin >> n >> m ;
    z = p.size() ;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < m; i++)
	cin >> b[i], bad[b[i]] = 1;
    for (int i = n - 1; i >= 0; i--){
	l = a[i] ;
	for (int j = i - 1; j >= 0; j--)
	    l = __gcd(a[j], l) ;
	if (f(l) > 0){
	    for (int j = i; j >= 0; j--)
		a[j] = a[j] / l ;
	}
    }
    l = 0 ;
    for (int i = 0; i < n; i++)
	l = l - f(a[i]) ;
    cout << l ;
    return 0;
}


//in the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define at(i,j) (i<0 || j<0?0:dp[i][j])

using namespace std ;
const long long maxn = 1e6 + 5 ;

int z, n, m, l, s, x, y, maxx, pr[maxn], a[maxn], b[maxn];
unordered_map <int, bool> bad ;
vector <int> p ;

int f(int x){
    int b = 0, g = 0, t = 0;
    for (int i = 0; i < z; i++){
	if (p[i] > x)
	    break ;
	t = 0 ;
	while (x % p[i] == 0)
	    x = x / p[i], t++ ;
	if (bad[p[i]])
	    b = b + t ;
	else
	    g = g + t ;
    }
    if (x > 1){
	if (bad[x])
	    b++ ;
	else
	    g++ ;
    }
    return b - g ;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    fill(pr, pr + maxn, 1) ;
    pr[1] = 0, pr[2] = 1 ;
    for (int i = 2; i <= 100000 / 2; i++){
	if (pr[i]){
	    p.pb(i) ;
	    l = 2;
	    while(l * i <= 100000 / 2)
		pr[l * i]  = 0, l++ ;
	}
    }
    l = 0 ;
    cin >> n >> m ;
    z = p.size() ;
    for (int i = 0; i < n; i++)
	cin >> a[i] ;
    for (int i = 0; i < m; i++)
	cin >> b[i], bad[b[i]] = 1;
    for (int i = n - 1; i >= 0; i--){
	l = a[i] ;
	for (int j = i - 1; j >= 0; j--)
	    l = __gcd(a[j], l) ;
	if (f(l) > 0){
	    for (int j = i; j >= 0; j--)
		a[j] = a[j] / l ;
	}
    }
    l = 0 ;
    for (int i = 0; i < n; i++)
	l = l - f(a[i]) ;
    cout << l ;
    return 0;
}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;

#define MOD 1000000007
#define INF 1000000
#define ll long long
#define slld(t) scanf("%lld",&t)
#define sd(t) scanf("%d",&t)
#define sld(t) scanf("%ld",&t)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%lld\n",t)
#define pcc pair<char,char>
#define pp pair<ll,int>
#define pll pair<ll,ll>
#define mp(a,b) make_pair(a,b)
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define F first
#define S second
#define pb(x) push_back(x)
#define MAX 150007
int A[5007];
map<int,int> imap;
int sum[5007];
int has[5007];
int get( int num)
{
	int ans = 0;
	int n = num;
	for( int i=2; i*i<=n; i++)
	{
		if(num%i==0)
		{
			int cnt = 0;
			while(num%i==0)
			{
				num /= i;
				cnt++;
			}
			if(imap[i])
			{
				ans -=  cnt;
			}
			else
			{
				ans += cnt;
			}
		}
	}
	if(num!=1)
	{
		if(imap[num])
		{
			ans -=  1;
		}
		else
		{
			ans += 1;
		}
	}

	return ans;

}

int main()
{
	int n,m;
	sd(n); sd(m);
	vector <int> v;
	for( int i=1; i<=n; i++)
	{
		sd(A[i]);
	}
	for( int i=1; i<=m; i++)
	{
		int k; sd(k);
		v.pb(k);
		imap[k] = 1;
	}
	for( int i=1; i<=n; i++)
	{
		has[i] = get(A[i]);
	}
	for( int i=n; i>=1; i--)
	{
		sum[i] = has[i] + sum[i+1];
	}
	int ans = sum[1];
	for( int i=0; i<v.size(); i++)
	{
		int t = v[i];
		int ans1 = 0;
		for( int j=1; j<=n; j++)
		{
			if(A[j]%t != 0)
			{
				break;
			}
			while(A[j]%t == 0)
			{
				A[j] /= t;
				ans1++;
			}
		}
		ans += ans1;
	}
	pd(ans);
}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;

#define MOD 1000000007
#define INF 1000000
#define ll long long
#define slld(t) scanf("%lld",&t)
#define sd(t) scanf("%d",&t)
#define sld(t) scanf("%ld",&t)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%lld\n",t)
#define pcc pair<char,char>
#define pp pair<ll,int>
#define pll pair<ll,ll>
#define mp(a,b) make_pair(a,b)
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define F first
#define S second
#define pb(x) push_back(x)
#define MAX 150007
int A[5007];
map<int,int> imap;
int sum[5007];
int has[5007];
int get( int num)
{
	int ans = 0;
	int n = num;
	for( int i=2; i*i<=n; i++)
	{
		if(num%i==0)
		{
			int cnt = 0;
			while(num%i==0)
			{
				num /= i;
				cnt++;
			}
			if(imap[i])
			{
				ans -=  cnt;
			}
			else
			{
				ans += cnt;
			}
		}
	}
	if(num!=1)
	{
		if(imap[num])
		{
			ans -=  1;
		}
		else
		{
			ans += 1;
		}
	}

	return ans;

}

int main()
{
	int n,m;
	sd(n); sd(m);
	vector <int> v;
	for( int i=1; i<=n; i++)
	{
		sd(A[i]);
	}
	for( int i=1; i<=m; i++)
	{
		int k; sd(k);
		v.pb(k);
		imap[k] = 1;
	}
	for( int i=1; i<=n; i++)
	{
		has[i] = get(A[i]);
	}
	for( int i=n; i>=1; i--)
	{
		sum[i] = has[i] + sum[i+1];
	}
	int ans = sum[1];
	cout<<ans<<endl;
	/*for( int i=1; i<=n; i++)
	{
		cout<<has[i]<<" ";
	}
	cout<<endl;
	for( int i=0; i<v.size(); i++)
	{
		int t = v[i];
		int ans1 = 0;
		vector <int> g;
		g.clear();
		for( int j=1; j<=n; j++)
		{
			if(A[j]%t != 0)
			{
				break;
			}
			int cnt = 0;
			while(A[j]%t == 0)
			{
				A[j] /= t;
				cnt++;
			}
			g.pb(cnt);
		}
		for( int j=0; j<g.size(); j++)
		{
			cout<<g[j]<<" ";
		}
		cout<<endl;
		for( int j=1; j<=32; j++)
		{
			for( int h=0; h<g.size(); h++)
			{
				if(g[h]>0)
				{
					g[h] -= 1;
					ans1++;
				}
				else
				{
					break;
				}
			}
		}
		ans += ans1;
	}
	pd(ans);*/
}


#include<bits/stdc++.h>
#include <math.h>
using namespace std;

#define MOD 1000000007
#define INF 1000000
#define ll long long
#define slld(t) scanf("%lld",&t)
#define sd(t) scanf("%d",&t)
#define sld(t) scanf("%ld",&t)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%lld\n",t)
#define pcc pair<char,char>
#define pp pair<ll,int>
#define pll pair<ll,ll>
#define mp(a,b) make_pair(a,b)
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define F first
#define S second
#define pb(x) push_back(x)
#define MAX 300007
int A[5007];
map<int,int> imap;
int sum[5007];
int has[5007];
int pre[5007];
int get( int num)
{
	int ans = 0;
	int n = num;
	for( int i=2; i*i<=n; i++)
	{
		if(num%i==0)
		{
			int cnt = 0;
			while(num%i==0)
			{
				num /= i;
				cnt++;
			}
			if(imap[i])
			{
				ans -=  cnt;
			}
			else
			{
				ans += cnt;
			}
		}
	}
	if(num!=1)
	{
		if(imap[num])
		{
			ans -=  1;
		}
		else
		{
			ans += 1;
		}
	}

	return ans;

}

int main()
{
	int n,m;
	sd(n); sd(m);
	vector <int> v;
	for( int i=1; i<=n; i++)
	{
		sd(A[i]);
	}
	for( int i=1; i<=m; i++)
	{
		int k; sd(k);
		v.pb(k);
		imap[k] = 1;
	}
	for( int i=1; i<=n; i++)
	{
		has[i] = get(A[i]);
	}
	for( int i=n; i>=1; i--)
	{
		sum[i] = has[i] + sum[i+1];
	}
	int ans = sum[1];
//	cout<<ans<<endl;
	pre[1] = A[1];
	for( int i=2; i<=n; i++)
	{
		pre[i] = __gcd(pre[i-1],A[i]);
	}
	int ans1 = 0; int lag = 1;
	for( int i=n; i>=1; i--)
	{
	//	cout<<i<<" "<<ans1<<endl;
		int num = pre[i] / lag;
	//	cout<<num<<endl;
		if(num==1)
		{
			continue;
		}
		else
		{
			int a=0,b=0;
			int num1 = num;
			for( int j=2; j*j<=num; j++)
			{
				if(num1%j==0)
				{
					int cnt = 0;
					while(num1%j==0)
					{
						num1 /= j;
						cnt++;
					}
					if(imap[j])
					{
						a += cnt;
					}
					else
					{
						b += cnt;
					}
				}
			}
			if(num1!=1)
			{
				if(imap[num1])
				{
					a++;
				}
				else
				{
					b++;
				}
			}
		//	cout<<i<<" "<<a<<" "<<b<<endl;
			if(a>b)
			{
				ans1 += (i)*(a-b);
				lag = pre[i];
			}
			else
			{
				continue;
			}
		}
	}
	ans += ans1;
	pd(ans);
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i,j)       for(int i = 0 ; i < j ; i++)
#define REPNM(i,j,k)   for(int i = j ; i < k ; i++)
#define RREP(i,j,k)    for(int i = j ; i >=k ; i--)
#define A    first
#define B    second
#define pb   push_back
#define mp   make_pair
#define PII pair<int , int>
#define VI  vector<int>
#define VPII vector<PII>
#define MEM(i,j)   memset(i , j , sizeof i)
#define ALL(i)     i.begin() , i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie() , cout.sync_with_stdio(0)
///------------------------------------------------------------
#define int long long
typedef long long LL;
#define MAP 500000
#define MAX 5050
#define INF 0x3f3f3f3f

set<int> bad;
int p[MAP] , po = 0 , dv[MAP];
void pre(){
    REPNM(i , 2 , MAP){
        if(dv[i] == 0) p[po ++] = i , dv[i] = i;
        REP(j , po){
            LL tmp = p[j];
            if(tmp * i >= MAP) break;
            dv[tmp * i] = tmp;
            if(i % tmp == 0) break;
        }
    }
}

int n , m , x[MAX] , dp[MAX] , g[MAX];

int P(int now){
    if(now < MAP) return dv[now];
    REP(i , po){
        LL tmp = p[i];
        if(tmp * tmp > now) return now;
        if(now % tmp == 0) return tmp;
    }
    return now;
}
int F(int now){
    int val = 0;
    while(now != 1){
        int tmp = P(now);
        if(bad.find(tmp) != bad.end()) val --;
        else val ++;
        now /= tmp;
    }
    return val;
}

int32_t main(){
    IOS;
    pre();
    cin >> n >> m;
    REP(i , n) cin >> x[i];
    int tmc;
    REP(i , m) cin >> tmc , bad.insert(tmc);
    int ans = 0;

    g[0] = x[0];
    REPNM(i , 1 , n) g[i] = __gcd(g[i - 1] , x[i]);

    RREP(i , n - 1 , 0){
        dp[i] = -F(g[i]) * (i + 1);
        REPNM(j , i + 1 , n){
            int tmp = g[i] / g[j];
            dp[i] = max(dp[i] , dp[j] + (-F(tmp) * (i + 1)));
        }
        ans = max(ans , dp[i]);
        // DB4("dp[" , i , "] = " , dp[i]);
    }
    // DBGG("ans = " , ans);
    REP(i , n) ans += F(x[i]);
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	int maxfloor;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	rep(i,0,n) {
		leftmax[i] = -1;
		rightmax[i] = -1;
	}
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}
	 maxfloor = -1;
	rep(i,0,n){
		if(leftmax[i] != -1){
			maxfloor = i;
		}
	}
	if(maxfloor == -1){
		cout << 0 << endl;
		return 0;
	}
	if(n == 1 || maxfloor == 0){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,maxfloor){
		//cout << "came here " << endl;
		if(leftmax[i] == -1){
			//cout << "came here " << endl;
			dp[i][0] = min(dp[i-1][0]+1,dp[i-1][1]+1+m+1);
			dp[i][1] = min(dp[i-1][1]+1,dp[i-1][0]+1+m+1);
			continue;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}

	cout << min(dp[maxfloor-1][0]+leftmax[maxfloor]+1,dp[maxfloor-1][1]+(m+1-rightmax[maxfloor])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	int maxfloor;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	rep(i,0,n) {
		leftmax[i] = -1;
		rightmax[i] = -1;
	}
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}
	int maxfloor = -1;
	rep(i,0,n){
		if(leftmax[i] != -1){
			maxfloor = i;
		}
	}
	if(maxfloor == -1){
		cout << 0 << endl;
		return 0;
	}
	if(n == 1 || maxfloor == 0){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,maxfloor){
		//cout << "came here " << endl;
		if(leftmax[i] == -1){
			//cout << "came here " << endl;
			dp[i][0] = min(dp[i-1][0]+1,dp[i-1][1]+1+m+1);
			dp[i][1] = min(dp[i-1][1]+1,dp[i-1][0]+1+m+1);
			continue;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}

	cout << min(dp[maxfloor-1][0]+leftmax[maxfloor]+1,dp[maxfloor-1][1]+(m+1-rightmax[maxfloor])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	int maxfloor;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	rep(i,0,n) {
		leftmax[i] = -1;
		rightmax[i] = -1;
	}
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}
	rep(i,0,n){
		if(leftmax[i] != -1){
			maxfloor = i;
		}
	}
	//cout << "maxfloor " << maxfloor << " " << leftmax[2] <<  endl;
	if(n == 1 || maxfloor == 0){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,maxfloor){
		//cout << "came here " << endl;
		if(leftmax[i] == -1){
			dp[i][0] = dp[i-1][0]+1;
			dp[i][1] = dp[i-1][1]+1;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}
	/*if(leftmax[n-1] == -1){
		cout << min(dp[n-2][0],dp[n-2][1]);
		return 0;
	}*/

	cout << min(dp[maxfloor-1][0]+leftmax[maxfloor]+1,dp[maxfloor-1][1]+(m+1-rightmax[maxfloor])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	int maxfloor;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	rep(i,0,n) {
		leftmax[i] = -1;
		rightmax[i] = -1;
	}
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}

	rep(i,0,n){
		if(leftmax[i] != -1){
			maxfloor = i;
		}
	}
	if(n == 1 || maxfloor == 0){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,maxfloor){
		//cout << "came here " << endl;
		if(leftmax[i] == -1){
			//cout << "came here " << endl;
			dp[i][0] = min(dp[i-1][0]+1,dp[i-1][1]+1+m+1);
			dp[i][1] = min(dp[i-1][1]+1,dp[i-1][0]+1+m+1);
			continue;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}

	cout << min(dp[maxfloor-1][0]+leftmax[maxfloor]+1,dp[maxfloor-1][1]+(m+1-rightmax[maxfloor])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	int maxfloor;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	rep(i,0,n) {
		leftmax[i] = -1;
		rightmax[i] = -1;
	}
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}
	rep(i,0,n){
		if(leftmax[i] != -1){
			maxfloor = i;
		}
	}
	//cout << "maxfloor " << maxfloor << " " << leftmax[2] <<  endl;
	if(n == 1 || maxfloor == 0){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,maxfloor){
		cout << "came here " << endl;
		if(leftmax[i] == -1){
			dp[i][0] = dp[i-1][0]+1;
			dp[i][1] = dp[i-1][1]+1;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}
	/*if(leftmax[n-1] == -1){
		cout << min(dp[n-2][0],dp[n-2][1]);
		return 0;
	}*/

	cout << min(dp[maxfloor-1][0]+leftmax[maxfloor]+1,dp[maxfloor-1][1]+(m+1-rightmax[maxfloor])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}

	if(n == 1){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,n-1){

		if(leftmax[i] == -1){
			dp[i][0] = dp[i-1][0]+1;
			dp[i][1] = dp[i-1][1]+1;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}
	if(leftmax[n-1] == -1){
		cout << min(dp[n-2][0],dp[n-2][1]);
		return 0;
	}

	cout << min(dp[n-2][0]+leftmax[n-1]+1,dp[n-2][1]+(m+1-rightmax[n-1])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define brep(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define SZ(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
string house[20];

int main(){

	int n,m;
	cin >> n >> m;
	int maxfloor;
	for(int i = n-1;i >= 0;i--) cin >> house[i];

	int leftmax[20];
	int rightmax[20];
	rep(i,0,n) {
		leftmax[i] = 0;
		rightmax[i] = 0;
	}
	int dp[20][2];
	rep(i,0,n){
		dp[i][0] = dp[i][1] = INT_MAX;
	}
	rep(i,0,n){

		int flag = 0;
		rep(j,0,house[i].length()){

			if(house[i][j] == '1' && flag == 0){

				flag = 1;
				rightmax[i] = j;
			}
			if(house[i][j] == '1') leftmax[i] = j;
		}
	}
	 maxfloor = -1;
	rep(i,0,n){
		if(leftmax[i] != 0){
			maxfloor = i;
		}
	}
	if(maxfloor == -1){
		cout << 0 << endl;
		return 0;
	}
	if(n == 1 || maxfloor == 0){
		if(leftmax[0] == -1){
			cout << 0 << endl;
			return 0;
		}
		cout << leftmax[0] << endl;
		return 0;
	}
	dp[0][0] = 2*leftmax[0];
	dp[0][1] = m+1;
	rep(i,1,maxfloor){
		//cout << "came here " << endl;
		if(leftmax[i] == 0){
			//cout << "came here " << endl;
			dp[i][0] = min(dp[i-1][0]+1,dp[i-1][1]+1+m+1);
			dp[i][1] = min(dp[i-1][1]+1,dp[i-1][0]+1+m+1);
			continue;
		}
		dp[i][0] = min(dp[i-1][0]+2*leftmax[i]+1,dp[i-1][1]+m+2);
		dp[i][1] = min(dp[i-1][0]+m+2,dp[i-1][1]+(m+1-rightmax[i])*2+1);
	}
	/*rep(i,0,maxfloor){

		cout << dp[i][0] << " " << dp[i][1] << endl;
	}
	*/
	cout << min(dp[maxfloor-1][0]+leftmax[maxfloor]+1,dp[maxfloor-1][1]+(m+1-rightmax[maxfloor])+1);
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	freopen("abc","r",stdin);
	int i,j,k,l,n,m,a[16][103],stair[17],ans=0,num=0;
	double mid,h;
	char c;
	scanf("%d%d",&n,&m);
	memset(stair,0,sizeof(stair));
	if(m%2==1) mid=(m+2)/2+1;
	else mid=(m+2)/2+0.5;
	for(i=1;i<=n;i++)
	{
		h=0;
		for(j=1;j<=m+2;j++)
		{
			c=getchar();
			if(c=='\n')	c=getchar();
			a[i][j]=c-'0';
			if(a[i][j]==1)	num++;
			h+=(j-mid)*a[i][j];
		}
		if(h>0)	stair[i-1]=1;
		else if(h<0)stair[i-1]=0;
		else if(h==0)
		{
			stair[i]=!stair[i-1];
		}
	}
	stair[n]=0;
	for(i=n;i>=1;i--)
	{
		if(stair[i]==0)
		{
			k=1;
			for(j=1;j<=m+1;j++)
			{
				if(a[i][j]==1)	{k=j;	num--;}
			}
			if(num==0)	{ans=ans+k-1;	break;}
			else if(stair[i-1]==0)	ans=ans+2*(k-1)+1;
			else if(stair[i-1]==1)	ans=ans+m+2;
		}
		if(stair[i]==1)
		{
			k=m+2;
			for(j=m+1;j>1;j--)
			{
				if(a[i][j]==1)	{k=j;	num--;}
			}
			if(num==0){ans=ans+m+2-k;	break;}
			else if(stair[i-1]==1)	ans=ans+2*(m+2-k)+1;
			else if(stair[i-1]==0)	ans=ans+m+2;
		}
	}
	printf("%d",ans);
	fclose(stdin);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef unsigned long long ull;

int n,m,f[16],l[16],v[103][2],last;
char x;
bool flag;

int solve(int cur,int par) {
	if(cur==last) {
		if(f[cur]==-1) return 0;
		else {
			if(par) return m+1-f[cur];
			else return l[cur];
		}
	}
	int &ret = v[cur][par];
	if(ret!=-1) return ret;
	ret = 0;
	if(f[cur]!=-1) {
		if(par) {
			ret += m+1-f[cur];
			ret += min(f[cur]+1+solve(cur-1,0),m+2-f[cur]+solve(cur-1,1));
		} else {
			ret += l[cur];
			ret += min(l[cur]+1+solve(cur-1,0),m+2-l[cur]+solve(cur-1,1));
		}
	} else ret = 1+solve(cur-1,par);
	return ret;
}

int main() {

	ios::sync_with_stdio(false);
	memset(f,-1,sizeof(f));
	memset(l,-1,sizeof(l));
	memset(v,-1,sizeof(v));
	cin >> n >> m;

	for(int i=0;i<n;i++) {
		for(int j=0;j<m+2;j++) {
			cin >> x;
			if(x=='1') {
				if(!flag) {
					last = i;
					flag = 1;
				}
				if(f[i]==-1) f[i] = j;
				l[i] = j;
			}
		}
	}

	if(flag) cout << solve(n-1,0) << endl;
	else cout << 0 << endl;

}


#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
int main() {
	int n,m,j=0,cnt=0;
	bool flag = 0;
	string s,x;
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		int mini=0,maxi=0;
		s = " ";
		cin >> x;
		s += x;
		for(int i=2;i<m+2;i++) {
			if(s[i]=='1' && !mini) mini = i;
			else if(s[i]=='1') maxi = i;
		}
		v1.push_back(mini);
		if(maxi==0) v2.push_back(mini);
		else v2.push_back(maxi);
	}
	reverse(v1.begin(),v1.end());
	reverse(v2.begin(),v2.end());
	while(v1[j]==0) {
		j++;
	}
	if(j+1<n) {
		    cnt = v2[j]-1;
        	int last = v2[j];
			for(int i=j+1;i<n;i++) {
			if(v1[i]==0) {
				flag = 1;
				continue;
			}
			if(m+2-last+(m+2-v2[i])+1<last+v1[i]-1) {
				cnt += m+2-last+(m+2-v2[i])+1;
				last = v2[i];
			} else {
				cnt += last+v1[i]-1;
				last = v1[i];
			}
			if(flag) {
				cnt++;
				flag = 0;
			}
			cnt += (v2[i]-v1[i]);
		}
    }
	cout << max(0,cnt) << endl;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
int main() {
	int n,m,j=0,cnt=0,last=0,f=0;
	string s,x;
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		int mini=0,maxi=0;
		s = " ";
		cin >> x;
		s += x;
		for(int i=2;i<m+2;i++) {
			if(s[i]=='1' && !mini) mini = i;
			else if(s[i]=='1') maxi = i;
		}
		v1.push_back(mini);
		if(maxi==0) v2.push_back(mini);
		else v2.push_back(maxi);
	}
	reverse(v1.begin(),v1.end());
	reverse(v2.begin(),v2.end());
	while(v1[j]==0) {
		j++;
		f++;
	}
	if(j<n) {

		cnt = f+v2[j]-1;
		f = 0;
        last = v2[j];
	}
	if(j+1<n) {
			for(int i=j+1;i<n;i++) {
			if(v1[i]==0) {
				f++;
				continue;
			}
			if(m+2-last+(m+2-v2[i])+1<last+v1[i]-1) {
				cnt += m+2-last+(m+2-v2[i])+1;
				last = v2[i];
			} else {
				cnt += last+v1[i]-1;
				last = v1[i];
			}
			if(f) {
				cnt += f;
				f = 0;
			}
			cnt += (v2[i]-v1[i]);
		}
    }
	cout << max(0,cnt) << endl;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
int main() {
	int n,m;
	string s,x;
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		int mini=0,maxi=0;
		s = " ";
		cin >> x;
		s += x;
		for(int i=2;i<m+2;i++) {
			if(s[i]=='1' && !mini) mini = i;
			else if(s[i]=='1') maxi = i;
		}
		v1.push_back(mini);
		if(maxi==0) v2.push_back(mini);
		else v2.push_back(maxi);
	}
	reverse(v1.begin(),v1.end());
	reverse(v2.begin(),v2.end());
	int cnt = v2[0]-1;
	int last = v2[0];
	for(int i=1;i<n;i++) {
		if(m+2-last+(m+2-v2[i])+1<last+v1[i]-1) {
			cnt += m+2-last+(m+2-v2[i])+1;
			last = v2[i];
		} else {
			cnt += last+v1[i]-1;
			last = v1[i];
		}
		cnt += (v2[i]-v1[i]);
	}
	cout << cnt << endl;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
vector<int> d;
vector<int> in;
int main() {
	int n,m,cnt=0,c=0;
	string s,x;
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		int mini=0,maxi=0;
		s = " ";
		cin >> x;
		s += x;
		for(int j=2;j<=m+2;j++) {
			if(s[j]=='1' && !mini) mini = j;
			else if(s[j]=='1') maxi = j;
		}
		if(mini) {
			v1.push_back(mini);
			if(maxi==0) v2.push_back(mini);
			else v2.push_back(maxi);
			d.push_back(c);
			in.push_back(i);
			c++;
		}
	}
	if(d.size()==0) {
		cout << 0;
		return 0;
	}
	reverse(d.begin(),d.end());
	reverse(in.begin(),in.end());
	int last = n-1;
	cnt += last-in[0];
	last = in[0];
	cnt += v2[d[0]]-1;
	int ind = v2[d[0]];
	if(d.size()>1) {
		for(int i=1;i<d.size();i++) {
			cnt += last-in[i]-1;
			last = in[i];
			if(m+2-ind+(m+2-v2[d[i]])+1<ind+v1[d[i]]-1) {
					cnt += m+2-ind+(m+2-v2[d[i]])+1;
					ind = v1[d[i]];
			} else {
					cnt += ind+v1[d[i]]-1;
					ind = v2[d[i]];
			}
			cnt += (v2[d[i]]-v1[d[i]]);
		}
	}
	cout << cnt << endl;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
int main() {
	int n,m,j=0,cnt=0,last=0;
	bool flag = 0;
	string s,x;
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		int mini=0,maxi=0;
		s = " ";
		cin >> x;
		s += x;
		for(int i=2;i<m+2;i++) {
			if(s[i]=='1' && !mini) mini = i;
			else if(s[i]=='1') maxi = i;
		}
		v1.push_back(mini);
		if(maxi==0) v2.push_back(mini);
		else v2.push_back(maxi);
	}
	reverse(v1.begin(),v1.end());
	reverse(v2.begin(),v2.end());
	while(v1[j]==0) {
		j++;
	}
	if(j<n) {
		cnt = v2[j]-1;
        last = v2[j];
	}
	if(j+1<n) {
			for(int i=j+1;i<n;i++) {
			if(v1[i]==0) {
				flag = 1;
				continue;
			}
			if(m+2-last+(m+2-v2[i])+1<last+v1[i]-1) {
				cnt += m+2-last+(m+2-v2[i])+1;
				last = v2[i];
			} else {
				cnt += last+v1[i]-1;
				last = v1[i];
			}
			if(flag) {
				cnt++;
				flag = 0;
			}
			cnt += (v2[i]-v1[i]);
		}
    }
	cout << max(0,cnt) << endl;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
int main() {
	int n,m,j=0,cnt=0,last=0,f=0;
	string s,x;
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		int mini=0,maxi=0;
		s = " ";
		cin >> x;
		s += x;
		for(int i=2;i<=m+2;i++) {
			if(s[i]=='1' && !mini) mini = i;
			else if(s[i]=='1') maxi = i;
		}
		v1.push_back(mini);
		if(maxi==0) v2.push_back(mini);
		else v2.push_back(maxi);
	}
	reverse(v1.begin(),v1.end());
	reverse(v2.begin(),v2.end());
	while(v1[j]==0) {
		j++;
		f++;
	}
	if(j<n) {

		cnt = f+v2[j]-1;
		f = 0;
        last = v2[j];
	}
	if(j+1<n) {
			for(int i=j+1;i<n;i++) {
			if(v1[i]==0) {
				f++;
				continue;
			}
			if(m+2-last+(m+2-v2[i])+1<last+v1[i]-1) {
				cnt += m+2-last+(m+2-v2[i])+1;
				last = v2[i];
			} else {
				cnt += last+v1[i]-1;
				last = v1[i];
			}
			if(f) {
				cnt += f;
				f = 0;
			}
			cnt += (v2[i]-v1[i]);
		}
    }
	cout << max(0,cnt) << endl;
}


/*Till Death do us Part......... */

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define couts(a) cout<<a<<"\n"
#define fr(i,a,b) for(int i=a;i<b;++i)
#define ioS ios_base::sync_with_stdio(0)
#define coutd(a,b) cout<<a<<" "<<b<<"\n"

//============================DEBUG==========================================
#define trace(a) cout<<#a<<": "<<a<<endl
#define trace2(a,b) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<endl;
#define trace3(a,b,c) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<" | "<<#c<<": "<<c<<endl;
//============================================================================

typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll>pi;
typedef long long int ll;
typedef vector<int> vi;

const ll MAXN=1e6+10;
const int MOD=1e9+7;

string s[20];
int ans;
int n,m;;
int sf;

void recur(int fno, int sum,int sd)
{
	int ind=0;

	if(fno==sf)
	{
		//trace2(fno,sd);

		if(sd==0)
		{
			fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
			ans=min(sum+ind,ans);
		}
		else
		{
			for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
			ind=m+1-ind;
			ans=min(ans,sum+ind);

		}

		return ;
	}

	if(sd==0)
	{
		fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
		recur(fno-1,sum+2*(ind)+1,0);
		recur(fno-1,sum+m+2,1);
	}
	else
	{
		recur(fno-1,sum+m+2,0);

		for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
		ind=m+1-ind;
		recur(fno-1,sum+2*(ind)+1,1);
	}
}

int main()
{

	cin>>n>>m;

	fr(i,0,n)cin>>s[i];
	ans=1e9;

	fr(i,0,n)fr(j,0,m+2)if(s[i][j]=='1'){sf=i;goto hell;}
	hell:;
	//trace(sf);

	recur(n-1,0,0);

	cout<<ans<<endl;

}


/*Till Death do us Part......... */

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define couts(a) cout<<a<<"\n"
#define fr(i,a,b) for(int i=a;i<b;++i)
#define ioS ios_base::sync_with_stdio(0)
#define coutd(a,b) cout<<a<<" "<<b<<"\n"

//============================DEBUG==========================================
#define trace(a) cout<<#a<<": "<<a<<endl
#define trace2(a,b) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<endl;
#define trace3(a,b,c) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<" | "<<#c<<": "<<c<<endl;
//============================================================================

typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll>pi;
typedef long long int ll;
typedef vector<int> vi;

const ll MAXN=1e6+10;
const int MOD=1e9+7;

string s[20];
int ans;
int n,m;;
int sf;

void recur(int fno, int sum,int sd)
{
	int ind=0;

	if(fno==sf)
	{
		//trace2(fno,sd);

		if(sd==0)
		{
			fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
			ans=min(sum+ind,ans);
		}
		else
		{
			ind=m+1;
			for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
			ind=m+1-ind;
			ans=min(ans,sum+ind);

		}

		return ;
	}

	if(sd==0)
	{
		fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
		recur(fno-1,sum+2*(ind)+1,0);
		recur(fno-1,sum+m+2,1);
	}
	else
	{
		recur(fno-1,sum+m+2,0);

		for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
		ind=m+1-ind;
		recur(fno-1,sum+2*(ind)+1,1);
	}
}

int main()
{

	cin>>n>>m;

	fr(i,0,n)cin>>s[i];
	ans=1e9;
	sf=-1;
	fr(i,0,n)fr(j,0,m+2)
	{

		if(s[i][j]=='1'){sf=i;goto hell;}
	}
	hell:;

	//if(sf==n-1){cout<<0<<endl;re 0;}
	//trace(sf);
	if(sf==-1)sf=n-1;

	recur(n-1,0,0);

	cout<<ans<<endl;

}


/*Till Death do us Part......... */

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define couts(a) cout<<a<<"\n"
#define fr(i,a,b) for(int i=a;i<b;++i)
#define ioS ios_base::sync_with_stdio(0)
#define coutd(a,b) cout<<a<<" "<<b<<"\n"

//============================DEBUG==========================================
#define trace(a) cout<<#a<<": "<<a<<endl
#define trace2(a,b) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<endl;
#define trace3(a,b,c) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<" | "<<#c<<": "<<c<<endl;
//============================================================================

typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll>pi;
typedef long long int ll;
typedef vector<int> vi;

const ll MAXN=1e6+10;
const int MOD=1e9+7;

string s[20];
int ans;
int n,m;;
int sf;

void recur(int fno, int sum,int sd)
{
	int ind=0;

	if(fno==sf)
	{
		//trace2(fno,sd);

		if(sd==0)
		{
			fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
			ans=min(sum+ind,ans);
		}
		else
		{
			ind=m+1;
			for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
			ind=m+1-ind;
			ans=min(ans,sum+ind);

		}

		return ;
	}

	if(sd==0)
	{
		fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
		recur(fno-1,sum+2*(ind)+1,0);
		recur(fno-1,sum+m+2,1);
	}
	else
	{
		recur(fno-1,sum+m+2,0);
		ind=m+1;
		for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
		ind=m+1-ind;
		recur(fno-1,sum+2*(ind)+1,1);
	}
}

int main()
{

	cin>>n>>m;

	fr(i,0,n)cin>>s[i];
	ans=1e9;
	sf=-1;
	fr(i,0,n)fr(j,0,m+2)
	{

		if(s[i][j]=='1'){sf=i;goto hell;}
	}
	hell:;

	//if(sf==n-1){cout<<0<<endl;re 0;}
	//trace(sf);
	if(sf==-1)sf=n-1;

	recur(n-1,0,0);

	cout<<ans<<endl;

}


/*Till Death do us Part......... */

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define couts(a) cout<<a<<"\n"
#define fr(i,a,b) for(int i=a;i<b;++i)
#define ioS ios_base::sync_with_stdio(0)
#define coutd(a,b) cout<<a<<" "<<b<<"\n"

//============================DEBUG==========================================
#define trace(a) cout<<#a<<": "<<a<<endl
#define trace2(a,b) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<endl;
#define trace3(a,b,c) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<" | "<<#c<<": "<<c<<endl;
//============================================================================

typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll>pi;
typedef long long int ll;
typedef vector<int> vi;

const ll MAXN=1e6+10;
const int MOD=1e9+7;

string s[20];
int ans;
int n,m;;
int sf;

void recur(int fno, int sum,int sd)
{
	int ind=0;

	if(fno==sf)
	{
		//trace2(fno,sd);

		if(sd==0)
		{
			fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
			ans=min(sum+ind,ans);
		}
		else
		{
			for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
			ind=m+1-ind;
			ans=min(ans,sum+ind);

		}

		return ;
	}

	if(sd==0)
	{
		fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
		recur(fno-1,sum+2*(ind)+1,0);
		recur(fno-1,sum+m+2,1);
	}
	else
	{
		recur(fno-1,sum+m+2,0);

		for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
		ind=m+1-ind;
		recur(fno-1,sum+2*(ind)+1,1);
	}
}

int main()
{

	cin>>n>>m;

	fr(i,0,n)cin>>s[i];
	ans=1e9;

	fr(i,0,n)fr(j,0,m+2)if(s[i][j]=='1'){sf=i;goto hell;}
	hell:;

	if(sf==n-1){cout<<0<<endl;re 0;}
	//trace(sf);

	recur(n-1,0,0);

	cout<<ans<<endl;

}


/*Till Death do us Part......... */

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define couts(a) cout<<a<<"\n"
#define fr(i,a,b) for(int i=a;i<b;++i)
#define ioS ios_base::sync_with_stdio(0)
#define coutd(a,b) cout<<a<<" "<<b<<"\n"

//============================DEBUG==========================================
#define trace(a) cout<<#a<<": "<<a<<endl
#define trace2(a,b) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<endl;
#define trace3(a,b,c) cout<<#a<<": "<<a<<" | " <<#b<<": "<<b<<" | "<<#c<<": "<<c<<endl;
//============================================================================

typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll>pi;
typedef long long int ll;
typedef vector<int> vi;

const ll MAXN=1e6+10;
const int MOD=1e9+7;

string s[20];
int ans;
int n,m;;

void recur(int fno, int sum,int sd)
{
	int ind=0;
	if(fno==0)
	{
		//trace2(sum,sd);
		if(sd==0)
		{
			fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
			ans=min(sum+ind,ans);
		}
		else
		{
			for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
			ind=m+1-ind;
			ans=min(ans,sum+ind);

		}

		return ;
	}

	if(sd==0)
	{
		fr(i,0,m+2)if(s[fno][i]=='1')ind=i;
		recur(fno-1,sum+2*(ind)+1,0);
		recur(fno-1,sum+m+2,1);
	}
	else
	{
		recur(fno-1,sum+m+2,0);

		for(int i=m+1;i>=1;i--)if(s[fno][i]=='1')ind=i;
		ind=m+1-ind;
		recur(fno-1,sum+2*(ind)+1,1);
	}
}

int main()
{

	cin>>n>>m;

	fr(i,0,n)cin>>s[i];
	ans=1e9;
	recur(n-1,0,0);

	cout<<ans<<endl;

}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f<=l)
    {
        val+=wat[l][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=999999999;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1') wat[i][0]=j;
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        dfs(n,0,0);
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[19][102];
int wat[19][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        int sum=0;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1')
                {
                    wat[i][0]=j;
                    sum++;
                }
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(sum==0)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1') wat[i][0]=j;
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        dfs(n,0,0);
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[19][101];
int wat[19][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        int sum=0;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1')
                {
                    wat[i][0]=j;
                    sum++;
                }
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(sum==0)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
int fffff;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        int sum=0;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1')
                {
                    wat[i][0]=j;
                    sum++;
                }
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(sum==0)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        int sum=0;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1')
                {
                    wat[i][0]=j;
                    sum++;
                }
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(sum==0)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
int fffff;
long long fxffff;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        int sum=0;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1')
                {
                    wat[i][0]=j;
                    sum++;
                }
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(sum==0)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[19][200];
int wat[19][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        int sum=0;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1')
                {
                    wat[i][0]=j;
                    sum++;
                }
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(sum==0)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f<=l)
    {
        val+=wat[l][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1') wat[i][0]=j;
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        dfs(n,0,0);
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f<=l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1') wat[i][0]=j;
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        dfs(n,0,0);
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int n,m,l;
char map[16][101];
int wat[16][2];
int ans;
void dfs(int f,int pos,int val)
{
    //cout<<val<<endl;
    if(f==l)
    {
        val+=wat[f][pos];
        if(val<ans) ans=val;
        //cout<<val<<endl;
        return;
    }
    dfs(f-1,(pos+1)%2,val+m+2);
    dfs(f-1,pos,val+wat[f][pos]*2+1);
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        l=1;
        ans=99999999;
        memset(map,0,sizeof(map));
        memset(wat,0,sizeof(wat));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=m+1;j++)
            {
               cin>>map[i][j];
            }
            for(int j=1;j<=m;j++)
                if(map[i][j]=='1') wat[i][0]=j;
            for(int j=m;j>=1;j--)
                if(map[i][j]=='1') wat[i][1]=m-j+1;
            if(wat[i][0]==0&&l==i)
                l++;
            //cout<<wat[i][0]<<" "<<wat[i][1]<<endl;
        }
        if(l>n)
            cout<<0<<endl;
        else
        {
            dfs(n,0,0);
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#define LL long long
#define eps 1e-6
using namespace std;

int dir1[2][2] = {0, 1, 0, -1};
int dir2[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m;
int vis[20][105][1510];

struct node
{
    int x, y, step;
    int c[20][105];
    int ans;
};

node a, b;
int num;

bool check(node a)
{
    if(a.x<0 || a.x>=n || a.y<0 || a.y>=m+2 || vis[a.x][a.y][a.ans] >= 2)
        return false;
    return true;
}
int bfs()
{
    queue<node> que;
    que.push(a);
    vis[0][0][a.ans] = 1;
    while(!que.empty())
    {
        b = que.front();
        //printf("%d %d %d %d\n", b.x, b.y, b.step, b.ans);
        que.pop();
        if(b.y == 0 || b.y == m+1)
        {
            for(int i=0; i<4; i++)
            {
                a = b;
                a.x += dir2[i][0];
                a.y += dir2[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                if(a.ans == 0)
                {
                    return a.step;
                }
                que.push(a);
            }
        }
        else if(b.y >0 && b.y <= m)
        {
            for(int i=0; i<2; i++)
            {
                a = b;
                a.x += dir1[i][0];
                a.y += dir1[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                if(a.ans == 0)
                {
                    return a.step;
                }
                que.push(a);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        num = 0;
        char c[105];
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%s", c);
            for(int j=0; j<m+2; j++)
            {
                a.c[i][j] = c[j]-'0';
                if(a.c[i][j])
                    num++;
            }
        }
        a.x = n-1;
        a.y = 0;
        a.step = 0;
        a.ans = num;
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#define LL long long
#define eps 1e-6
#define INF 0x3f3f3f3f
using namespace std;

int n, m;
char a[20][105];
int num[20];
int l[20];
int r[20];
int dp[20][2];
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        memset(num, 0, sizeof(num));
        for(int i=0; i<n;i++)
        {
            scanf("%s", a[i]);
            l[i] = 0;
            r[i] = m+1;
            for(int j=0; j<m+2; j++)
            {
                if(a[i][j] == '1')
                {
                    num[i]++;
                    if(num[i] == 1)
                        l[i] = j;
                }
            }
            for(int j=m+1; j>=0; j--)
            {
                if(a[i][j] == '1')
                {
                    r[i] = j;
                    break;
                }
            }
        }
        //for(int i=0; i<n; i++)
          //  printf("%d %d\n", l[i], r[i]);
        int x = 0;
        while(!num[x] && x<n)
        {
            x++;
        }
        dp[n-1][0] = r[n-1];
        dp[n-1][1] = INF;
        //printf("%d %d\n", dp[n-1][0], dp[n-1][1]);
        for(int i=n-2; i>=x; i--)
        {
            dp[i][0] = min(dp[i+1][0]+1+r[i+1]+r[i], dp[i+1][1]+l[i+1]+r[i]+1);
            dp[i][1] = min(dp[i+1][1]+m+1-l[i+1]+m+2-l[i], dp[i+1][0]+m+1-r[i+1]+m+2-l[i]);
            //printf("%d %d\n", dp[i][0], dp[i][1]);
        }
        printf("%d\n", min(dp[x][0], dp[x][1]));
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#define LL long long
#define eps 1e-6
using namespace std;

int dir1[2][2] = {0, 1, 0, -1};
int dir2[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m;
int vis[20][105][1510];

struct node
{
    int x, y, step;
    int c[20][105];
    int ans;
};

node a, b;

bool check(node a)
{
    if(a.x<0 || a.x>=n || a.y<0 || a.y>=m+2 || vis[a.x][a.y][a.ans] >= 2)
        return false;
    return true;
}
int bfs()
{
    queue<node> que;
    que.push(a);
    vis[n-1][0][a.ans] = 1;
    while(!que.empty())
    {
        b = que.front();
        //printf("%d %d %d %d\n", b.x, b.y, b.step, b.ans);
        que.pop();
        if(b.y == 0 || b.y == m+1)
        {
            for(int i=0; i<4; i++)
            {
                a = b;
                a.x += dir2[i][0];
                a.y += dir2[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                if(a.ans == 0)
                {
                    return a.step;
                }
                que.push(a);
            }
        }
        else if(b.y >0 && b.y <= m)
        {
            for(int i=0; i<2; i++)
            {
                a = b;
                a.x += dir1[i][0];
                a.y += dir1[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(a.ans == 0)
                {
                    return a.step;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                que.push(a);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        int num = 0;
        char c[105];
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%s", c);
            for(int j=0; j<m+2; j++)
            {
                a.c[i][j] = c[j]-'0';
                if(a.c[i][j])
                    num++;
            }
        }
        if(num == 0)
        {
            printf("0\n");
            continue;
        }
        a.x = n-1;
        a.y = 0;
        a.step = 0;
        a.ans = num;
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#define LL long long
#define eps 1e-6
using namespace std;

int dir1[2][2] = {0, 1, 0, -1};
int dir2[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m;
int vis[20][105][1510];

struct node
{
    int x, y, step;
    int c[20][105];
    int ans;
};

node a, b;

bool check(node a)
{
    if(a.x<0 || a.x>=n || a.y<0 || a.y>=m+2 || vis[a.x][a.y][a.ans] >= 2)
        return false;
    return true;
}
int bfs()
{
    queue<node> que;
    que.push(a);
    vis[n-1][0][a.ans] = 1;
    while(!que.empty())
    {
        b = que.front();
        //printf("%d %d %d %d\n", b.x, b.y, b.step, b.ans);
        que.pop();
        if(b.y == 0 || b.y == m+1)
        {
            for(int i=0; i<4; i++)
            {
                a = b;
                a.x += dir2[i][0];
                a.y += dir2[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                if(a.ans == 0)
                {
                    return a.step;
                }
                que.push(a);
            }
        }
        else if(b.y >0 && b.y <= m)
        {
            for(int i=0; i<2; i++)
            {
                a = b;
                a.x += dir1[i][0];
                a.y += dir1[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(a.ans == 0)
                {
                    return a.step;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                que.push(a);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        int num = 0;
        char c[105];
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%s", c);
            for(int j=0; j<m+2; j++)
            {
                a.c[i][j] = c[j]-'0';
                if(a.c[i][j])
                    num++;
            }
        }
        a.x = n-1;
        a.y = 0;
        a.step = 0;
        a.ans = num;
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#define LL long long
#define eps 1e-6
using namespace std;

int dir1[2][2] = {0, 1, 0, -1};
int dir2[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m;
int vis[20][105][1510];

struct node
{
    int x, y, step;
    int c[20][105];
    int ans;
};

node a, b;

bool check(node a)
{
    if(a.x<0 || a.x>=n || a.y<0 || a.y>=m+2 || vis[a.x][a.y][a.ans] >= 3)
        return false;
    return true;
}
int bfs()
{
    queue<node> que;
    que.push(a);
    vis[0][0][a.ans] = 1;
    while(!que.empty())
    {
        b = que.front();
        //printf("%d %d %d %d\n", b.x, b.y, b.step, b.ans);
        que.pop();
        if(b.y == 0 || b.y == m+1)
        {
            for(int i=0; i<4; i++)
            {
                a = b;
                a.x += dir2[i][0];
                a.y += dir2[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                if(a.ans == 0)
                {
                    return a.step;
                }
                que.push(a);
            }
        }
        else if(b.y >0 && b.y <= m)
        {
            for(int i=0; i<2; i++)
            {
                a = b;
                a.x += dir1[i][0];
                a.y += dir1[i][1];
                a.step++;
                if(a.c[a.x][a.y] == 1)
                {
                    a.c[a.x][a.y] = 0;
                    a.ans--;
                }
                if(!check(a))
                    continue;
                vis[a.x][a.y][a.ans]++;
                if(a.ans == 0)
                {
                    return a.step;
                }
                que.push(a);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        int num = 0;
        char c[105];
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%s", c);
            for(int j=0; j<m+2; j++)
            {
                a.c[i][j] = c[j]-'0';
                if(a.c[i][j])
                    num++;
            }
        }
        a.x = n-1;
        a.y = 0;
        a.step = 0;
        a.ans = num;
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);

    int n,m;
    cin>>n>>m;
    vector<string> r(n,"");

    rep(i,0,n-1)
        cin>>r[i];

    vector<vi> dp(n,vi(2,0));
    vi _left(n,m+5);
    vi _right(n,-1);

    rep(i,0,n-1)
    {
        rep(j,0,m+1)
        {
            if(r[i][j]=='1')
             {   _left[i]= min((int)j,_left[i]);
                _right[i]=max((int)j,_right[i]);
             }
        }
    }

    if(_right[0]!=-1)
    {
        dp[0][0]= _right[0];
         dp[0][1]= (m+1)- _left[0];
    }
    else
    {
         dp[0][0]= 0;
         dp[0][1]= 0;
    }

    rep(i,1,n-1)
    {
        if(_right[i]!=-1)

        {   dp[i][0] = min(_right[i] +_right[i]+1+dp[i-1][0],m+1 + 1 +dp[i-1][1]);
            dp[i][1] = min((m+1)- _left[i] + (m+1) -_left[i] +1 + dp[i-1][1],m+1 + 1 +dp[i-1][0]);
        }

        else
        {
            dp[i][0] = min(1+dp[i-1][0],m+1 + 1 +dp[i-1][1]);
            dp[i][1] = min(1 + dp[i-1][1],m+1 + 1 +dp[i-1][0]);
        }

    }

    cout << dp[n-1][0];

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;
int n,m,cnt[200],a[25][200],ans = 123456789,last;
void solve(int row,int col,int cur){
     cerr << row << " " << col << " " << cur << endl;
   if(row < 0){
    ans = min(ans,cur);
    return;
   }
   int vis = 0,step = 0;
   if(col == 0){
    for(int i = 0 ; i < m + 2 && vis < cnt[row] ;i++){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);
    return;
   }
    for(int i = m + 1 ; i >= 0 && vis < cnt[row] ;i--){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + m + 2);
    solve(row - 1,m + 1,cur + 2*step + 1);

}
int main()
{
    cin >> n >> m;
    char c;
    for(int i = 0;i < n ; i++)
        for(int j = 0 ; j < m + 2; j++)
            cin >> c, a[i][j] = c - '0' , cnt[i] += a[i][j];
    last= 0;
    while(cnt[last] == 0 && last < n)last++;
    cerr << last << endl;
    solve(n - 1,0,0);
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;
int n,m,cnt[200],a[25][200],ans = 123456789,last;
void solve(int row,int col,int cur){

   if(row < 0){
    ans = min(ans,cur);
    return;
   }
   int vis = 0,step = 0;
   if(col == 0){
    for(int i = 0 ; i < m + 2 && vis < cnt[row] ;i++){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);
    return;
   }
    for(int i = m + 1 ; i >= 0 && vis < cnt[row] ;i--){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);

}
int main()
{
    cin >> n >> m;
    char c;
    for(int i = 0;i < n ; i++)
        for(int j = 0 ; j < m + 2; j++)
            cin >> c, a[i][j] = c - '0' , cnt[i] += a[i][j];
    last= 0;
    while(cnt[last] == 0 && last < n)last++;
    cerr << last << endl;
    solve(n - 1,0,0);
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;
int n,m,cnt[200],a[25][200],ans = 123456789,last;
void solve(int row,int col,int cur){
   if(row < 0){
       cerr << cur << endl;
    ans = min(ans,cur);
    return;
   }
   int vis = 0,step = 0;
   if(col == 0){
    for(int i = 0 ; i < m + 2 && vis < cnt[row] ;i++){
       vis += a[row][i];
       step++;
    }
    if(cnt[row])step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);
    return;
   }
    for(int i = m + 1 ; i >= 0 && vis < cnt[row] ;i--){
       vis += a[row][i];
       step++;
    }
    if(cnt[row])
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + m + 2);
    solve(row - 1,m + 1,cur + 2*step + 1);

}
int main()
{
    cin >> n >> m;
    char c;
    for(int i = 0;i < n ; i++)
        for(int j = 0 ; j < m + 2; j++)
            cin >> c, a[i][j] = c - '0' , cnt[i] += a[i][j];
    last= 0;
    while(cnt[last] == 0 && last < n)last++;
    if(last == n){
        cout << 0;
        return 0;
    }
    solve(n - 1,0,0);
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;
int n,m,cnt[200],a[25][200],ans = 123456789,last;
void solve(int row,int col,int cur){
   if(row < 0){
    ans = min(ans,cur);
    return;
   }
   int vis = 0,step = 0;
   if(col == 0){
    for(int i = 0 ; i < m + 2 && vis < cnt[row] ;i++){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);
    return;
   }
    for(int i = m + 1 ; i >= 0 && vis < cnt[row] ;i--){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + m + 2);
    solve(row - 1,m + 1,cur + 2*step + 1);

}
int main()
{
    cin >> n >> m;
    char c;
    for(int i = 0;i < n ; i++)
        for(int j = 0 ; j < m + 2; j++)
            cin >> c, a[i][j] = c - '0' , cnt[i] += a[i][j];
    last= 0;
    while(cnt[last] == 0 && last < n)last++;
    if(last == n){
        cout << 0;
        return 0;
    }
    solve(n - 1,0,0);
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;
int n,m,cnt[200],a[25][200],ans = 123456789;
void solve(int row,int col,int cur){
   if(row < 0){
    ans = min(ans,cur);
    return;
   }
   int vis = 0,step = 0;
   if(col == 0){
    for(int i = 0 ; i < m + 2 && vis < cnt[row] ;i++){
       vis += a[row][i];
       step++;
    }
    if(row == 0)
    {
        solve(row - 1,0,cur + step);
        return;
    }
    step--;
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);
    return;
   }
    for(int i = m + 1 ; i >= 0 && vis < cnt[row] ;i--){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == 0)
    {
        solve(row - 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);

}
int main()
{
    cin >> n >> m;
    char c;
    for(int i = 0;i < n ; i++)
        for(int j = 0 ; j < m + 2; j++)
            cin >> c, a[i][j] = c - '0' , cnt[i] += a[i][j];

    solve(n - 1,0,0);
    cout << ans ;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vll vector< ll >
#define vii vector< pair<ll,ll> >
#define vi vector<int>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
using namespace std;
int n,m,cnt[200],a[25][200],ans = 123456789,last;
void solve(int row,int col,int cur){
   if(row < 0){
    ans = min(ans,cur);
    return;
   }
   int vis = 0,step = 0;
   if(col == 0){
    for(int i = 0 ; i < m + 2 && vis < cnt[row] ;i++){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + 2*step + 1);
    solve(row - 1,m + 1,cur + m + 2);
    return;
   }
    for(int i = m + 1 ; i >= 0 && vis < cnt[row] ;i--){
       vis += a[row][i];
       step++;
    }
    step--;
    if(row == last)
    {
        solve(- 1,0,cur + step);
        return;
    }
    solve(row - 1,0,cur + m + 2);
    solve(row - 1,m + 1,cur + 2*step + 1);

}
int main()
{
    cin >> n >> m;
    char c;
    for(int i = 0;i < n ; i++)
        for(int j = 0 ; j < m + 2; j++)
            cin >> c, a[i][j] = c - '0' , cnt[i] += a[i][j];
    last= 0;
    while(cnt[last] == 0 && last < n)last++;
    if(last == n - 1 && cnt[last] == 0){
        cout << 0;
        return 0;
    }
    solve(n - 1,0,0);
    cout << ans ;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll n;ll m;
    cin>>n>>m;
    char a[n+1][m+3];
    rev(i,n,1)
    {
        rep(j,1,m+2)cin>>a[i][j];
    }
    ll ldis[n+1];
    ll rdis[n+1];
    bool visit[n+1];
    rep(i,1,n)
    {
        visit[i]=0;
        rep(j,1,m+2){
        if(a[i][j]=='1')
        {
            visit[i]=1;
            break;
        }
        }
        if(!visit[i])continue;
        ldis[i]=1;
        while(a[i][ldis[i]]!='1')ldis[i]++;
        rdis[i]=1;
        while(a[i][m-rdis[i]+3]!='1')rdis[i]++;
    }
    ll sum=0;
    bool dir=0;

    rep(i,1,n)
    {
        if(i<n){if(!visit[i]){sum++;continue;}}
        else{if(!visit[i])continue;}
        ll pos;
        if(!dir){sum+=(m-rdis[i]+2);pos=(m-rdis[i]+2+1);}
        else{sum+=m-ldis[i]+2;pos=(ldis[i]);}
        if(i<n)
        {
            ll k=i+1;
            while(!visit[k]){k++;if(k>n)break;}
            //cout<<i<<" "<<k<<endl;
            if(k<=n)
            {
                if(pos-1+m-rdis[k]+2<m-pos+2+m-ldis[k]+2){dir=0;sum+=pos-1;}
                else{dir=1;sum+=m-pos+2;}
            }
            else
            {
                break;
            }
            sum++;
        }
    }

    cout<<sum;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll n;ll m;
    cin>>n>>m;
    char a[n+1][m+3];
    rev(i,n,1)
    {
        rep(j,1,m+2)cin>>a[i][j];
    }
    ll ldis[n+1];
    ll rdis[n+1];
    bool visit[n+1];
    rep(i,1,n)
    {
        visit[i]=0;
        rep(j,1,m+2){
        if(a[i][j]=='1')
        {
            visit[i]=1;
            break;
        }
        }
        if(!visit[i])continue;
        ldis[i]=1;
        while(a[i][ldis[i]]!='1')ldis[i]++;
        rdis[i]=1;
        while(a[i][m-rdis[i]+3]!='1')rdis[i]++;
    }
    ll sum=0;
    bool dir=0;
    bool any=1;
    rep(i,1,n)if(visit[i])any=0;
    if(any){cout<<0;return 0;}
    rep(i,1,n)
    {
        if(i<n){if(!visit[i]){sum++;continue;}}
        else{if(!visit[i])continue;}
        ll pos;
        if(!dir){sum+=(m-rdis[i]+2);pos=(m-rdis[i]+2+1);}
        else{sum+=m-ldis[i]+2;pos=(ldis[i]);}
        if(i<n)
        {
            ll k=i+1;
            while(!visit[k]){k++;if(k>n)break;}
            //cout<<i<<" "<<k<<endl;
            if(k<=n)
            {
                if(pos-1+m-rdis[k]+2<m-pos+2+m-ldis[k]+2){dir=0;sum+=pos-1;}
                else{dir=1;sum+=m-pos+2;}
            }
            else
            {
                break;
            }
            sum++;
        }
    }

    cout<<sum;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll n;ll m;
    cin>>n>>m;
    char a[n+1][m+1];
    rev(i,n,1)
    {
        char k;
        cin>>k;
        rep(j,1,m)cin>>a[i][j];
        cin>>k;
    }
    ll ldis[n+1];
    ll rdis[n+1];
    bool visit[n+1];
    rep(i,1,n)
    {
        visit[i]=0;
        rep(j,1,m){
        if(a[i][j]=='1')
        {
            visit[i]=1;
            break;
        }
        }
        if(!visit[i])continue;
        ldis[i]=1;
        while(a[i][ldis[i]]!='1')ldis[i]++;
        rdis[i]=1;
        while(a[i][m-rdis[i]+1]!='1')rdis[i]++;
    }
    ll sum=0;
    bool dir=0;
    rep(i,1,n)
    {
        if(i<n){if(!visit[i]){sum++;continue;}}
        else{if(!visit[i])continue;}
        ll pos;
        if(!dir){sum+=(m-rdis[i]+1);pos=(m-rdis[i]+1);}
        else{sum+=m-ldis[i]+1;pos=(ldis[i]);}
        if(i<n)
        {
            ll k=i+1;
            while(!visit[k]){k++;if(k>n)break;}
            if(k<=n)
            {
                if(pos+m-rdis[k]+1<m-pos+1+m-ldis[k]+1){dir=0;sum+=pos;}
                else{dir=1;sum+=m-pos+1;}
            }
            else
            {
                break;
            }
            sum++;
        }
    }

    cout<<sum;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll n;ll m;
    cin>>n>>m;
    char a[n+1][m+1];
    rev(i,n,1)
    {
        char k;
        cin>>k;
        rep(j,1,m)cin>>a[i][j];
        cin>>k;
    }
    ll ldis[n+1];
    ll rdis[n+1];
    bool visit[n+1];
    rep(i,1,n)
    {
        visit[i]=0;
        rep(j,1,m){
        if(a[i][j]=='1')
        {
            visit[i]=1;
            break;
        }
        }
        if(!visit[i])continue;
        ldis[i]=1;
        while(a[i][ldis[i]]!='1')ldis[i]++;
        rdis[i]=1;
        while(a[i][m-rdis[i]+1]!='1')rdis[i]++;
    }
    ll sum=0;
    bool dir=0;
    bool any=1;
    rep(i,1,n)
    {
        if(visit[i])any=0;
    }
    if(any){cout<<0;return 0;}
    ll dpl[n+1];
    ll dpr[n+1];
    dpr[1]=m+1;
    if(visit[1])dpl[1]=(m-rdis[1]+1)*2;
    else dpl[1]=0;
    ll fin=n;
    while(!visit[fin])fin--;
    rep(i,2,fin-1)
    {
        if(!visit[i]){dpl[i]=dpl[i-1]+1;dpr[i]=dpr[i-1]+1;}
        else
        {
            dpl[i]=1+min(dpr[i-1]+m+1,dpl[i-1]+2*(m-rdis[i]+1));
            dpr[i]=1+min(dpl[i-1]+m+1,dpr[i-1]+2*(m-ldis[i]+1));
        }
    }
    cout<<1+min(dpl[fin-1]+m-rdis[fin]+1,dpr[fin-1]+m-ldis[fin]+1);

    return 0;
}

/*

   if(i<n){if(!visit[i]){sum++;continue;}}
        else{if(!visit[i])continue;}
        ll pos;
        if(!dir){sum+=(m-rdis[i]+1);pos=(m-rdis[i]+1);}
        else{sum+=m-ldis[i]+1;pos=(ldis[i]);}
        if(i<n)
        {
            ll k=i+1;
            while(!visit[k]){k++;if(k>n)break;}
            if(k<=n)
            {
                if(pos+m-rdis[k]+1<m-pos+1+m-ldis[k]+1){dir=0;sum+=pos;}
                else{dir=1;sum+=m-pos+1;}
            }
            else
            {
                break;
            }
            sum++;
        }

*/


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll n;ll m;
    cin>>n>>m;
    char a[n+1][m+1];
    rev(i,n,1)
    {
        char k;
        cin>>k;
        rep(j,1,m)cin>>a[i][j];
        cin>>k;
    }
    ll ldis[n+1];
    ll rdis[n+1];
    bool visit[n+1];
    rep(i,1,n)
    {
        visit[i]=0;
        rep(j,1,m){
        if(a[i][j]=='1')
        {
            visit[i]=1;
            break;
        }
        }
        if(!visit[i])continue;
        ldis[i]=1;
        while(a[i][ldis[i]]!='1')ldis[i]++;
        rdis[i]=1;
        while(a[i][m-rdis[i]+1]!='1')rdis[i]++;
    }
    ll sum=0;
    bool any=1;
    rep(i,1,n)
    {
        if(visit[i])any=0;
    }
    if(any){cout<<0;return 0;}
    if(n==1){cout<<m-rdis[1]+1;return 0;}
    ll dpl[n+1];
    ll dpr[n+1];
    dpr[1]=m+1;
    if(visit[1])dpl[1]=(m-rdis[1]+1)*2;
    else dpl[1]=0;
    ll fin=n;
    while(!visit[fin])fin--;
    if(fin==1){cout<<m-rdis[1]+1;return 0;}
    rep(i,2,fin-1)
    {
        if(!visit[i]){dpl[i]=dpl[i-1]+1;dpr[i]=dpr[i-1]+1;}
        else
        {
            dpl[i]=1+min(dpr[i-1]+m+1,dpl[i-1]+2*(m-rdis[i]+1));
            dpr[i]=1+min(dpl[i-1]+m+1,dpr[i-1]+2*(m-ldis[i]+1));
        }
    }
    cout<<1+min(dpl[fin-1]+m-rdis[fin]+1,dpr[fin-1]+m-ldis[fin]+1);

    return 0;
}

/*

   if(i<n){if(!visit[i]){sum++;continue;}}
        else{if(!visit[i])continue;}
        ll pos;
        if(!dir){sum+=(m-rdis[i]+1);pos=(m-rdis[i]+1);}
        else{sum+=m-ldis[i]+1;pos=(ldis[i]);}
        if(i<n)
        {
            ll k=i+1;
            while(!visit[k]){k++;if(k>n)break;}
            if(k<=n)
            {
                if(pos+m-rdis[k]+1<m-pos+1+m-ldis[k]+1){dir=0;sum+=pos;}
                else{dir=1;sum+=m-pos+1;}
            }
            else
            {
                break;
            }
            sum++;
        }

*/


#include<bits/stdc++.h>
using namespace std;
const int maxn=2001;
int he[maxn],ne[maxn<<1],to[maxn<<1],cnt,de[maxn],md,n;
#define addedge(x,y) ne[++cnt]=he[x],he[x]=cnt,to[cnt]=y
vector<int>dd[maxn];
void dfs(int x,int fa)
{
	dd[de[x]].push_back(x);
	md=max(md,de[x]);
	for(int i=he[x];i;i=ne[i])
	{
		if(to[i]==fa)continue;
	    de[to[i]]=de[x]+1;
		dfs(to[i],x);
	}
}
void solve()
{
	cnt=0;md=0;
	cin>>n;
	for(int i=0;i<=n;i++)he[i]=0,dd[i].clear();
	for(int i=2;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		addedge(a,b),addedge(b,a);
	}
	cout<<"? "<<n<<" ";
	for(int i=1;i<=n;i++)cout<<i<<" ";cout<<"\n";
	fflush(stdout);
	int rt,d;cin>>rt>>d;
	de[rt]=0;
	dfs(rt,0);
	int emm=0;
	int a,b,aa=rt,ndd=0;
	for(int i=10;i>=0;i--)
	{
		int em=emm+(1<<i);
		if(em>d||em>md)continue;
//		cout<<em<<"!!!\n";
//        if(em<=d/2){emm=em;ndd=0;continue;}
		cout<<"? "<<dd[em].size()<<" ";
		for(int j=0;j<dd[em].size();j++)cout<<dd[em][j]<<" ";cout<<"\n";
		fflush(stdout);
		cin>>a>>b;
		if(b==d)emm=em,aa=a,ndd=1;
	}
//	cout<<aa<<"deafakioi\n";
	/*
	8
8 7
6 4
7 2
4 1
4 8
5 8
3 7
2 6
	*/
	if(ndd==0)
	{
		int em=emm;
		cout<<"? "<<dd[em].size()<<" ";
		for(int j=0;j<dd[em].size();j++)cout<<dd[em][j]<<" ";cout<<"\n";
		fflush(stdout);
		cin>>a>>b;
		if(b==d)emm=em,aa=a,ndd=1;
	}
	for(int i=0;i<=n;i++)dd[i].clear();
    a=aa;
    de[a]=0;
	dfs(a,0);
	cout<<"? "<<dd[d].size()<<" ";
	for(int j=0;j<dd[d].size();j++)cout<<dd[d][j]<<" ";cout<<"\n";
	fflush(stdout);
    int x,y;
	cin>>x>>y;
	assert(y==d);
	cout<<"! "<<a<<" "<<x<<"\n";
	fflush(stdout);
	string dfk;
	cin>>dfk;
	return;
}
int main()
{
	int t;
	cin>>t;
	while(t--)solve();
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2001;
int he[maxn],ne[maxn<<1],to[maxn<<1],cnt,de[maxn],md,n;
#define addedge(x,y) ne[++cnt]=he[x],he[x]=cnt,to[cnt]=y
vector<int>dd[maxn];
void dfs(int x,int fa)
{
	de[x]=de[fa]+1;
	for(int i=he[x];i;i=ne[i])
	{
		if(to[i]==fa)continue;
		dfs(to[i],x);
	}
	de[x]--;
	dd[de[x]].push_back(x);
	md=max(md,de[x]);
}
void solve()
{
	cnt=0;md=0;
	cin>>n;
	for(int i=1;i<=n;i++)he[i]=0,dd[i].clear();
	for(int i=2;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		addedge(a,b),addedge(b,a);
	}
	cout<<"? "<<n<<" ";
	for(int i=1;i<=n;i++)cout<<i<<" ";cout<<"\n";
	fflush(stdout);
	int rt,d;cin>>rt>>d;
	dfs(rt,0);
	int emm=0;
	int a,b,aa=rt;
	for(int i=10;i>=0;i--)
	{
		int em=emm+(1<<i);
		if(em>d||em>md)continue;
//		cout<<em<<"!!!\n";
        if(em<=d/2){emm=em;continue;}
		cout<<"? "<<dd[em].size()<<" ";
		for(int j=0;j<dd[em].size();j++)cout<<dd[em][j]<<" ";cout<<"\n";
		fflush(stdout);
		cin>>a>>b;
		if(b==d)emm=em,aa=a;
	}
//	cout<<emm<<"deafakioi\n";
	for(int i=1;i<=n;i++)dd[i].clear();
	/*
	8
8 7
6 4
7 2
4 1
4 8
5 8
3 7
//2 6
	*/
    a=aa;
	dfs(a,0);
	cout<<"? "<<dd[d].size()<<" ";
	for(int j=0;j<dd[d].size();j++)cout<<dd[d][j]<<" ";cout<<"\n";
	fflush(stdout);
    int x,y;
	cin>>x>>y;
	assert(y==d);
	cout<<"! "<<a<<" "<<x<<"\n";
	fflush(stdout);
	string dfk;
	cin>>dfk;
	return;
}
int main()
{
	int t;
	cin>>t;
	while(t--)solve();
}


#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)

const int N = 1e3 + 5;
int t, n;
int edg, head[N], to[N << 1], nxt[N << 1];
vector<int> q, S[N];
set<int> L;
int rt, len, u, d, s, h, dep[N], far[N];
char ret[15];

void solve(void);
void add_edge(int u, int v);
void query(const vector<int> &a, int &u, int &d);
void dfs(int u, int f = 0);
bool check(int x);

int main(void) {
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

void solve(void) {
    scanf("%d", &n), edg = 1, q.clear(), L.clear();
    for (int i = 1, u, v; i < n; i++)
        scanf("%d%d", &u, &v), add_edge(u, v), q.push_back(i);
    q.push_back(n), query(q, rt, len);
    for (int i = 0; i <= n; i++) S[i].clear();
    dep[0] = -1, dfs(rt);
    int l = (len + 1) / 2, r = len;
    while (l <= r) check(mid) ? l = mid + 1, h = u : r = mid - 1;
    for (int u = h; u ^ rt; u = far[u]) L.insert(u);
    q.clear(), d = len - dep[h];
    for (int x : S[d]) L.find(x) == L.end() ? q.push_back(x) : void();
    query(q, s, d), printf("! %d %d\n", s, h);
    fflush(stdout), scanf("%s", ret + 1), assert(ret[1] == 'C');
}

inline void add_edge(int u, int v) {
    to[++edg] = v, nxt[edg] = head[u], head[u] = edg;
    to[++edg] = u, nxt[edg] = head[v], head[v] = edg;
}

void query(const vector<int> &a, int &u, int &d) {
    printf("? %d", a.size());
    for (int x : a) printf(" %d", x);
    putchar('\n'), fflush(stdout), scanf("%d%d", &u, &d);
}

void dfs(int u, int f) {
    dep[u] = dep[f] + 1;
    far[u] = f;
    S[dep[u]].push_back(u);
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v ^ f) dfs(v, u);
    }
}

inline bool check(int k) {
    query(S[k], u, d);
    return d == len;
}


#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)

const int N = 1e3 + 5;
int t, n;
int edg, head[N], to[N << 1], nxt[N << 1];
vector<int> q, S[N];
set<int> L;
int rt, len, u, d, s, h, dep[N], far[N];

void solve(void);
void add_edge(int u, int v);
void query(const vector<int> &a, int &u, int &d);
void dfs(int u, int f = 0);
bool check(int x);

int main(void) {
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

void solve(void) {
    scanf("%d", &n), edg = 1, q.clear(), L.clear();
    for (int i = 1, u, v; i < n; i++)
        scanf("%d%d", &u, &v), add_edge(u, v), q.push_back(i);
    q.push_back(n), query(q, rt, len);
    for (int i = 0; i < n; i++) S[i].clear();
    dep[0] = -1, dfs(rt);
    int l = len / 2, r = len - 1;
    while (l < r) check(mid) ? l = mid + 1, h = u : r = mid - 1;
    for (int u = h; u ^ rt; u = far[u]) L.insert(u);
    q.clear(), d = len - dep[h];
    for (int x : S[d]) L.find(x) == L.end() ? q.push_back(x) : void();
    query(q, s, d), printf("! %d %d\n", s, h);
}

inline void add_edge(int u, int v) {
    to[++edg] = v, nxt[edg] = head[u], head[u] = edg;
    to[++edg] = u, nxt[edg] = head[v], head[v] = edg;
}

void query(const vector<int> &a, int &u, int &d) {
    printf("? %d", a.size());
    for (int x : a) printf(" %d", x);
    putchar('\n'), fflush(stdout), scanf("%d%d", &u, &d);
}

void dfs(int u, int f) {
    dep[u] = dep[f] + 1, far[u] = f, S[dep[u]].push_back(u);
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v ^ f) dfs(v, u);
    }
}

inline bool check(int k) {
    query(S[k], u, d);
    return d == len;
}


#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)

const int N = 1e3 + 5;
int t, n;
int edg, head[N], to[N << 1], nxt[N << 1];
vector<int> q, S[N];
set<int> L;
int rt, len, u, d, s, h, dep[N], far[N];
char ret[15];

void solve(void);
void add_edge(int u, int v);
void query(const vector<int> &a, int &u, int &d);
void dfs(int u, int f = 0);
bool check(int x);

int main(void) {
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

void solve(void) {
    scanf("%d", &n), q.clear(), L.clear();
    edg = 1, memset(head, 0, (n + 1) * sizeof(int));
    for (int i = 1, u, v; i < n; i++)
        scanf("%d%d", &u, &v), add_edge(u, v), q.push_back(i);
    q.push_back(n), query(q, rt, len), assert(~rt);
    for (int i = 0; i <= n; i++) S[i].clear();
    dep[0] = -1, dfs(rt);
    int l = (len + 1) / 2, r = len;
    while (l <= r) check(mid) ? l = mid + 1, h = u : r = mid - 1;
    for (int u = h; u ^ rt; u = far[u]) L.insert(u);
    q.clear(), d = len - dep[h];
    for (int x : S[d]) L.find(x) == L.end() ? q.push_back(x) : void();
    query(q, s, d), printf("! %d %d\n", s, h);
    fflush(stdout), scanf("%s", ret + 1), assert(ret[1] == 'C');
}

inline void add_edge(int u, int v) {
    to[++edg] = v, nxt[edg] = head[u], head[u] = edg;
    to[++edg] = u, nxt[edg] = head[v], head[v] = edg;
}

void query(const vector<int> &a, int &u, int &d) {
    printf("? %d", a.size());
    for (int x : a) printf(" %d", x);
    putchar('\n'), fflush(stdout), scanf("%d%d", &u, &d);
}

void dfs(int u, int f) {
    dep[u] = dep[f] + 1, far[u] = f, S[dep[u]].push_back(u);
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v ^ f) dfs(v, u);
    }
}

inline bool check(int k) {
    return !S[k].empty() ? query(S[k], u, d), assert(~u), d == len : false;
}


#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)

const int N = 1e3 + 5;
int t, n;
int edg, head[N], to[N << 1], nxt[N << 1];
vector<int> q, S[N];
set<int> L;
int rt, len, u, d, s, h, dep[N], far[N];
char ret[15];

void solve(void);
void add_edge(int u, int v);
void query(const vector<int> &a, int &u, int &d);
void dfs(int u, int f = 0);
bool check(int x);

int main(void) {
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

void solve(void) {
    scanf("%d", &n), edg = 1, q.clear(), L.clear();
    for (int i = 1, u, v; i < n; i++)
        scanf("%d%d", &u, &v), add_edge(u, v), q.push_back(i);
    q.push_back(n), query(q, rt, len);
    for (int i = 0; i < n; i++) S[i].clear();
    dep[0] = -1, dfs(rt);
    int l = (len + 1) / 2, r = len;
    while (l <= r) check(mid) ? l = mid + 1, h = u : r = mid - 1;
    for (int u = h; u ^ rt; u = far[u]) L.insert(u);
    q.clear(), d = len - dep[h];
    for (int x : S[d]) L.find(x) == L.end() ? q.push_back(x) : void();
    query(q, s, d), printf("! %d %d\n", s, h);
    fflush(stdout), scanf("%s", ret + 1), assert(ret[1] == 'C');
}

inline void add_edge(int u, int v) {
    to[++edg] = v, nxt[edg] = head[u], head[u] = edg;
    to[++edg] = u, nxt[edg] = head[v], head[v] = edg;
}

void query(const vector<int> &a, int &u, int &d) {
    printf("? %d", a.size());
    for (int x : a) printf(" %d", x);
    putchar('\n'), fflush(stdout), scanf("%d%d", &u, &d);
}

void dfs(int u, int f) {
    dep[u] = dep[f] + 1, far[u] = f, S[dep[u]].push_back(u);
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v ^ f) dfs(v, u);
    }
}

inline bool check(int k) {
    query(S[k], u, d);
    return d == len;
}


#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)

const int N = 1e3 + 5;
int t, n;
int edg, head[N], to[N << 1], nxt[N << 1];
vector<int> q, S[N];
set<int> L;
int rt, len, u, d, s, h, dep[N], far[N];
char ret[15];

void solve(void);
void add_edge(int u, int v);
void query(const vector<int> &a, int &u, int &d);
void dfs(int u, int f = 0);
bool check(int x);

int main(void) {
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

void solve(void) {
    scanf("%d", &n), edg = 1, q.clear(), L.clear();
    for (int i = 1, u, v; i < n; i++)
        scanf("%d%d", &u, &v), add_edge(u, v), q.push_back(i);
    q.push_back(n), query(q, rt, len), assert(rt != -1);
    for (int i = 0; i <= n; i++) S[i].clear();
    dep[0] = -1, dfs(rt);
    int l = (len + 1) / 2, r = len;
    while (l <= r) check(mid) ? l = mid + 1, h = u : r = mid - 1;
    for (int u = h; u ^ rt; u = far[u]) L.insert(u);
    q.clear(), d = len - dep[h];
    for (int x : S[d]) L.find(x) == L.end() ? q.push_back(x) : void();
    query(q, s, d), printf("! %d %d\n", s, h);
    fflush(stdout), scanf("%s", ret + 1), assert(ret[1] == 'C');
}

inline void add_edge(int u, int v) {
    to[++edg] = v, nxt[edg] = head[u], head[u] = edg;
    to[++edg] = u, nxt[edg] = head[v], head[v] = edg;
}

void query(const vector<int> &a, int &u, int &d) {
    printf("? %d", a.size());
    for (int x : a) printf(" %d", x);
    putchar('\n'), fflush(stdout), scanf("%d%d", &u, &d);
}

void dfs(int u, int f) {
    dep[u] = dep[f] + 1;
    far[u] = f;
    if (dep[u] < 0 || dep[u] > n) puts("error");
    S[dep[u]].push_back(u);
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v ^ f) dfs(v, u);
    }
}

inline bool check(int k) {
    query(S[k], u, d);
    return d == len;
}