import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

        public Node(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }
    }

    static int nodes;
    static int Parent [] ;
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        nodes = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[nodes + 1];
        Parent = new int[nodes + 1];
        int arr[] = new int[nodes + 1];
        int ans2[] = new int[nodes + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[nodes + 1];
        for (int i = 0; i <= nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == nodes) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    Parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {

            Stack <Integer> stack = new Stack<Integer>();

            Parent[1] = -1;
            printPath();
//            for (int i = a - 1; i >= 0; i--) {
//                System.out.print(ans2[i] + " ");
//            }
        }
    }

        public static void printPath() {
//        Rewrite the path in a new array to print it
        int[] path = new int[nodes];
//        we will start from the last node and go back by the stored parent of it
        int parentNode = nodes;
        int i = 0;
        while (parentNode != -1) {
            path[i++] = (parentNode);
            parentNode = Parent[parentNode];
        }
//        print out the path starting from the last node stored in the path array and go back
        for (; i > 1;) {
            System.out.print(path[--i] + " ");
        }
        System.out.println(path[--i]);
    }

}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

        public Node(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {

            Stack <Integer> stack = new Stack<Integer>();

            parent[1] = -1;
            int last = node;
            int a = 0;
            while (last != 1) {
                stack.push(last);
                last = parent[last];
            }
            stack.push(1);
            while (!stack.isEmpty()) {
                System.out.print(stack.pop() + " ");
            }
//            for (int i = a - 1; i >= 0; i--) {
//                System.out.print(ans2[i] + " ");
//            }
        }
    }
}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        long cost;

        public Node(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        long arr[] = new long[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if (o1.cost > o2.cost) {
                    return 1;
                } else if (o1.cost < o2.cost) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        arr[1] = 0;
        arr[0] = 0;
        parent[1] = -1;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            long cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            int last = node;
            int a = node;
            while (last != -1) {
                ans2[a] = last;
                last = parent[last];
                a--;
            }
            for (int i = a + 1; i <= node; i++) {
                System.out.print(ans2[i] + " ");
            }
        }
    }

}

/*

Input:
9
1 5 -6 7 9 -16 0 -2 2

Output:
3

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long [] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new long [nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1) {
            path[i++] = node+1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

        public Node(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static int[] totalCost;
    static boolean [] visited;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new int[nodes];
        visited = new boolean[nodes];
        visited[0] = true;
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (parent[node] != -1) {
            path[i++] = node;
            node = parent[node];
        }
        path[i] = 0;
        for (int j = i ; j >= 0; j--) {
            System.out.print(path[j] + 1 + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node] ){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]|| ! visited[node.node]){
                    totalCost[node.node] = newCost;
                    visited[node.node] = true;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

        public Node(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }
    }
}


//Sloved by Kuantayev Chingis
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<math.h>
#define fi(i,n) for(i=1;i<=n;i++)
#define fj(j,n) for(j=1;j<=n;j++)

using namespace std;
int main ()
{
freopen ("input.txt","r",stdin);
freopen ("output.txt","w",stdout);

int n,k,i,a[1005];
cin>>n>>k;
fi(i,n)
{
cin>>a[i];
}
while(a[k]!=1)
{
k++;
if(k>n)
{
k=1;
}
}
cout<<k;
return 0;
}


//Sloved by Kuantayev Chingis
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define fi(i,n) for(i=1;i<=n;i++)
#define fj(j,n) for(j=1;j<=n;j++)

using namespace std;
int main ()
{
/*freopen ("input.txt","r",stdin);
freopen ("output.txt","w",stdout);*/

int n,k,i,a[1005];
cin>>n>>k;
fi(i,n)
{
cin>>a[i];
}
for(i=k;i<=n;i++)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
else if(n==k)
{
fi(i,n)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
}
}
}
return 0;
}


//Sloved by Kuantayev Chingis
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define fi(i,n) for(i=1;i<=n;i++)
#define fj(j,n) for(j=1;j<=n;j++)

using namespace std;
int main ()
{
freopen ("input.txt","r",stdin);
freopen ("output.txt","w",stdout);

int n,k,i,a[1005];
cin>>n>>k;
fi(i,n)
{
cin>>a[i];
}
for(i=k;i<=n;i++)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
else if(n==k)
{
fi(i,n)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
}
}
}
return 0;
}


//Sloved by Kuantayev Chingis
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include <stdio.h>
#include<math.h>
#define fi(i,n) for(i=1;i<=n;i++)
#define fj(j,n) for(j=1;j<=n;j++)

using namespace std;
int main ()
{
freopen ("input.txt","r",stdin);
freopen ("output.txt","w",stdout);

int n,k,i,a[1005];
cin>>n>>k;
fi(i,n)
{
cin>>a[i];
}
for(i=k;i<=n;i++)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
else if(n==k)
{
fi(i,n)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
}
}
}
return 0;
}


//Sloved by Kuantayev Chingis
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define fi(i,n) for(i=1;i<=n;i++)
#define fj(j,n) for(j=1;j<=n;j++)

using namespace std;
int main ()
{

int n,k,i,a[1005];
cin>>n>>k;
fi(i,n)
{
cin>>a[i];
}
for(i=k;i<=n;i++)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
else if(n==k)
{
fi(i,n)
{
if(a[i]==1)
{
cout<<i;
return 0;
}
}
}
}
return 0;
}


 #include <fstream>
using namespace std;
fstream cin("input.txt");
ofstream cout("output.txt");
int main()
{
 int n,k,a[1000];
 cin>>n>>k;
 for(int i=0;i<n;++i)
   cin>>a[i];
 for(;!a[(k-1)%n];++k);
 cout<<(k-1)%n+1<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	int a[1005];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	while(!a[k%n])k=(k+1)%n;
	cout<<k+1<<endl;
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	int a[1005];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	while(!a[k%n])k=(k+1)%n;
	printf("%d\n",k+1);
	return 0;
 }


//B.cpp
//SmartCoder
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i=(c).begin(); i!=(c).end();i++)
#define present(c,x)  ( (c).find(x) !=(c).end())
#define cpresent(c,x) (find(all(c),x)!= (c).end() )
#define minEI(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxEI(x)  max_element(x.begin(),x.end())-(x).begin()

#define UNS(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acuSum(x)  accumulate(x.begin(),x.end(),0)
#define acuMul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main()
    {
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    int n, k;
    vector<int> v(n);
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
	{
	cin >> v[i];
	}
    //k--;
    for (int i = k-1; i < 2*n; ++i)
	{
	if(v[i%(n)]==1)
	    {
	cout<<i%(n)+1<<endl;
	return 0;
	    }
	}
    return 0;
    }


//B.cpp
//SmartCoder
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i=(c).begin(); i!=(c).end();i++)
#define present(c,x)  ( (c).find(x) !=(c).end())
#define cpresent(c,x) (find(all(c),x)!= (c).end() )
#define minEI(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxEI(x)  max_element(x.begin(),x.end())-(x).begin()

#define UNS(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acuSum(x)  accumulate(x.begin(),x.end(),0)
#define acuMul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main()
    {
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    int n, k;
    vector<int> v(n);
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
	{
	cin >> v[i];
	}
    //k--;
    for (int i = k; i < 2*n; ++i)
	{
	if(v[i%(k)]==1)
	    {
	cout<<i%(k)+1<<endl;
	return 0;
	    }
	}
    return 0;
    }


//B.cpp
//SmartCoder
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i=(c).begin(); i!=(c).end();i++)
#define present(c,x)  ( (c).find(x) !=(c).end())
#define cpresent(c,x) (find(all(c),x)!= (c).end() )
#define minEI(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxEI(x)  max_element(x.begin(),x.end())-(x).begin()

#define UNS(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acuSum(x)  accumulate(x.begin(),x.end(),0)
#define acuMul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main()
    {
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    int n, k;

    cin >> n >> k;
    vector<int> v(n + 100);
    for (int i = 0; i < n; ++i)
	{
	cin >> v[i];
	}

    int i = k - 1;
    while (true)
	{
	if (v[(i % n)] == 1)
	    {
	    cout << i % (n) + 1 << endl;
	    return 0;
	    }
	i++;
	}
    return 0;
    }


//B.cpp
//SmartCoder
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i=(c).begin(); i!=(c).end();i++)
#define present(c,x)  ( (c).find(x) !=(c).end())
#define cpresent(c,x) (find(all(c),x)!= (c).end() )
#define minEI(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxEI(x)  max_element(x.begin(),x.end())-(x).begin()

#define UNS(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acuSum(x)  accumulate(x.begin(),x.end(),0)
#define acuMul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main()
    {
    // freopen("input.txt", "rt", stdin);
    //  freopen("output.txt", "wt", stdout);
    int n, k;
    vector<int> v(n + 100);
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
	{
	cin >> v[i];
	}
    k--;
    int i = k;
    while (true)
	{
	if (v[i % (n)] == 1)
	    {
	    cout << i % (n) + 1 << endl;
	    return 0;
	    }
	i++;
	}
    return 0;
    }


//B.cpp
//SmartCoder
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i=(c).begin(); i!=(c).end();i++)
#define present(c,x)  ( (c).find(x) !=(c).end())
#define cpresent(c,x) (find(all(c),x)!= (c).end() )
#define minEI(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxEI(x)  max_element(x.begin(),x.end())-(x).begin()

#define UNS(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acuSum(x)  accumulate(x.begin(),x.end(),0)
#define acuMul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main()
    {
     freopen("input.txt", "rt", stdin);
      freopen("output.txt", "wt", stdout);
    int n, k;
    vector<int> v(n + 100);
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
	{
	cin >> v[i];
	}
    k--;
    int i = k;
    while (true)
	{
	if (v[i % (n)] == 1)
	    {
	    cout << i % (n) + 1 << endl;
	    return 0;
	    }
	i++;
	}
    return 0;
    }


#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;
int main()
{
    int n;
    int k,i;

    ifstream infile;
    ofstream myfile;
       myfile.open("output.txt");
    infile.open("input.txt");
    while(infile>>n &&infile>>k){
        int a[n];
        for(i=0;i<n;i++){
            infile >> a[i];
        }
        int m = k;
        if(a[k-1] == 1){
            myfile <<k<<endl;
        }
        else if(a[k-1] == 0){
            if(k == n){
                m = 0;
            }
            else {
                m = k;
            }
            for(i=m;i<n;i++){
                if(i == n){
                    m=0;
                    continue;
                }
                else {
                    if(a[i] == 1){
                    //  cout<<i+1<<endl;
                        myfile <<i+1<<endl;
                        break;
                    }
                }
            }
        }
    }
    myfile.close();
}


#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;
int main()
{
    int n;
    int k,i;

    ifstream infile;
    ofstream myfile;
    myfile.open("output.txt");
    infile.open("input.txt");
    while(infile>>n && infile>>k){
        int a[n];
        for(i=0;i<n;i++){
            infile >> a[i];
        }
        int m;
        if(a[k-1] == 1){
            myfile <<k<<endl;
        }
        else if(a[k-1] == 0){
            if(k == n){
                m = 0;
            }
            else {
                m = k;
            }
            for(i=m;i<n;i++){
                if(i == n-1 && a[i] == 1){
                    myfile <<i+1<<endl;
                    break;
                }
                if(i == n-1){
                    i=0;
                    continue;
                }
                else {
                    if(a[i] == 1){
                        myfile <<i+1<<endl;
                        break;
                    }
                }
            }
        }
    }
    myfile.close();
}


#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;
int main()
{
    int n;
    int k,i;

    ifstream infile;
    ofstream myfile;
       myfile.open("output.txt");
    infile.open("input.txt");
    while(infile>>n && infile>>k){
        int a[n];
        for(i=0;i<n;i++){
            infile >> a[i];
        }
        int m;
        if(a[k-1] == 1){
            myfile <<k<<endl;
        }
        else if(a[k-1] == 0){
        //  cout<<"sss"<<endl;
            if(k == n){
                m = 0;
            }
            else {
                m = k;
            }
            for(i=m;i<n;i++){
                if(i == n-1){
                    m=0;
                    continue;
                }
                else {
                    if(a[i] == 1){
                        myfile <<i+1<<endl;
                        break;
                    }
                }
            }
        }
    }
    myfile.close();
}


#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;
int main()
{
    int n;
    int k,i;

    ifstream infile;
    ofstream myfile;
    myfile.open("output.txt");
    infile.open("input.txt");
    while(infile>>n && infile>>k){
        int a[n];
        for(i=0;i<n;i++){
            infile >> a[i];
        }
        int m;
        if(a[k-1] == 1){
            myfile <<k<<endl;
        }
        else if(a[k-1] == 0){
            if(k == n){
                m = 0;
            }
            else {
                m = k;
            }
            i = m;
            while(a[i] != 1){
                if(i == n-1 && a[i] == 1){
                    myfile <<i+1<<endl;
                    break;
                }
                if(i == n-1){
                    i=0;
                    continue;
                }
                i++;
            }

            myfile <<i+1<<endl;

        }
    }
        myfile.close();
}


#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;
int main()
{
    int n;
    int k,i;

    ifstream infile;
    ofstream myfile;
    myfile.open("output.txt");
    infile.open("input.txt");
    while(infile>>n && infile>>k){
        int a[n];
        for(i=0;i<n;i++){
            infile >> a[i];
        }
        int m;
        if(a[k-1] == 1){
            myfile <<k<<endl;
        }
        else if(a[k-1] == 0){
            if(k == n){
                m = 0;
            }
            else {
                m = k;
            }
            for(i=m;i<n;i++){
                if(i == n-1 && a[i] == 1){
                    myfile <<i+1<<endl;
                    break;
                }
                if(i == n-1){
                    m=0;
                    continue;
                }
                else {
                    if(a[i] == 1){
                        myfile <<i+1<<endl;
                        break;
                    }
                }
            }
        }
    }
    myfile.close();
}


#include <cstdio>
#include <iostream>
using namespace std;

int N, K;
int s[2002];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    cin >> N >> K;
    for (int i = 0; i < N; ++i) cin >> s[i];

    for (int i = 0; i < N; ++i) s[N + i] = s[i];

    for (int i = N<<1; i >= 0; --i)
        if (s[i] == 1) s[i] = i % N;
        else s[i] = s[i + 1];

    cout << s[K+1]+1 << endl;

    return 0;
}// (++UH)++ :: agarwaen


#include <cstdio>
#include <iostream>
using namespace std;

int N, K;
int s[1000];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    cin >> N >> K;
    for (int i = 0; i < N; ++i) cin >> s[i];

    for (--K; !s[K]; K = (K + 1) % N);

    cout << K+1 << endl;

    return 0;
}// (++UH)++ :: agarwaen


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

int N, K;
int s[2002];

int main()
{
    cin >> N >> K;
    for (int i = 0; i < N; ++i) cin >> s[i];

    for (int i = 0; i < N; ++i) s[N + i] = s[i];

    for (int i = N<<1; i >= 0; --i)
        if (s[i] == 1) s[i] = i % N;
        else s[i] = s[i + 1];

    cout << s[K+1]+1 << endl;

    return 0;
}// (++UH)++ :: agarwaen


#include <cstdio>
#include <iostream>
using namespace std;

int N, K;
int s[1000];

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    cin >> N >> K;
    for (int i = 0; i < N; ++i) cin >> s[i];

    for (--K; !s[K]; K = (K + 1) % N);

    cout << K+1 << endl;

    return 0;
}// (++UH)++ :: agarwaen


#include<iostream>
#include <fstream>
using namespace std;
int a[1005],i,n,k;
main(){
       freopen("input.txt","rt",stdin);
       freopen("output.txt","wt",stdout);
         cin>>n>>k;
         for(i=0;i<n;i++){cin>>a[i];a[i+n]=a[i];}
         for(i=k-1;i<2*n;i++)
         if(a[i]==1){cout<<i%n+1<<endl;break;}
         }


#include<iostream>
#include <fstream>
using namespace std;
int a[2005],i,n,k;
main(){
       freopen("input.txt","rt",stdin);
       freopen("output.txt","wt",stdout);
         cin>>n>>k;
         for(i=0;i<n;i++){cin>>a[i];a[i+n]=a[i];}
         for(i=k-1;i<2*n;i++)
         if(a[i]==1){cout<<i%n+1<<endl;break;}
         }


#include<iostream>
#include <fstream>
using namespace std;
int a[1005],i,n,k;
main(){
       freopen("input.txt","rt",stdin);
       freopen("output.txt","wt",stdout);
         cin>>n>>k;
         for(i=0;i<n;i++){cin>>a[i];a[i+n]=a[i];}
         for(i=k;i<2*n;i++)
         if(a[i]==1){cout<<i%n+1<<endl;break;}
         }


#include<iostream>
#include <fstream>
using namespace std;
int a[1005],i,n,k;
main(){
      // freopen("input.txt","rt",stdin);
      // freopen("output.txt","wt",stdout);
         cin>>n>>k;
         for(i=0;i<n;i++){cin>>a[i];a[i+n]=a[i];}
         for(i=k-1;i<2*n;i++)
         if(a[i]==1){cout<<i%n+1<<endl;break;}
         }


#include <fstream>
using namespace std;
fstream cin("input.txt");
ofstream cout("output.txt");

int main() {
	int n,k;
	cin>>n>>k;
	int a[n],i;
	for(i=0;i<n;i++) {
		cin>>a[i];
	}
	for(i=k-1;;i++) {
		if(i==n) i=0;
		if(a[i]==1) {
			cout<<i+1<<endl;
			break;
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int n,k;
	cin>>n>>k;
	int a[n],i,j;
	for(i=0;i<n;i++) {
		cin>>a[i];
	}
	for(i=k-1;i<n;i++) {
		if(a[i]==1) {
			cout<<i+1<<endl;
			break;
		}
		else if(i==n-1) {
			i=-1;
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int n,k;
	cin>>n>>k;
	int a[n],i,j;
	for(i=0;i<n;i++) {
		cin>>a[i];
	}
	for(i=k-1;;i++) {
		if(i==n) i=0;
		if(a[i]==1) {
			cout<<i+1<<endl;
			break;
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int n,k;
	cin>>n>>k;
	int a[n],i;
	for(i=0;i<n;i++) {
		cin>>a[i];
	}
	for(i=k-1;i<n;i++) {
		if(a[i]==1) {
			cout<<i+1<<endl;
			break;
		}
		else if(i==n-1) {
			i=-1;
		}
	}
	return 0;
}


	#include <bits/stdc++.h>
	using namespace std;
	typedef  long long ll;
	int arr[1002];
	int main()
	{

		freopen("input.txt" , "r" , stdin);
		freopen("output.txt" , "w" , stdout);

		int a , b;
		cin >> a >> b;
		for(int i =1;i<=a;i++)
		{
			scanf("%d",arr+i);
		}

		if(arr[b] == 1)
			cout << "1";
		else
			cout << "2";
		return 0;

	}


	#include <bits/stdc++.h>
	using namespace std;
	typedef  long long ll;
	int arr[1002];
	int main()
	{

		freopen("input.txt" , "r" , stdin);
		//freopen("output.txt" , "w" , stdout);

		int a , b;
		cin >> a >> b;
		for(int i =1;i<=a;i++)
		{
			scanf("%d",arr+i);
		}

		if(arr[b] == 1)
			cout << "1";
		else
			cout << "2";
		return 0;

	}


#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
int arr[1002];
int main()
{

	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w" , stdout);

	int a , b;
	cin >> a >> b;
	for(int i =1;i<=a;i++)
	{
		scanf("%d",arr+i);
	}

	if(arr[i] == 1)
		cout << "1";
	else
		cout << "0";
	return 0;

}


	#include <bits/stdc++.h>
	using namespace std;
	typedef  long long ll;
	int arr[1002];
	int main()
	{

		freopen("input.txt" , "r" , stdin);
		//freopen("output.txt" , "w" , stdout);

		int a , b;
		cin >> a >> b;
		for(int i =1;i<=a;i++)
		{
			scanf("%d",arr+i);
		}
		int count = 0;
		for(int i = b +1 ; ;i++)
		{
			if(i == a+1)
				i = 1;
			count++;
			if(arr[i] == 1)break;
		}
		cout << count;
		return 0;

	}


	#include <bits/stdc++.h>
	using namespace std;
	typedef  long long ll;
	int arr[1002];
	int main()
	{

		freopen("input.txt" , "r" , stdin);
		freopen("output.txt" , "w" , stdout);

		int a , b;
		cin >> a >> b;
		for(int i =1;i<=a;i++)
		{
			scanf("%d",arr+i);
		}
		int count = 0;
		for(int i = b +1 ; ;i++)
		{
			if(i == a+1)
				i = 1;
			count++;
			if(arr[i] == 1)break;
		}
		cout << count;
		return 0;

	}


#include <bits/stdc++.h>
	using namespace std;
	typedef  long long ll;
	int arr[1002];
	int main()
	{

		freopen("input.txt" , "r" , stdin);
		freopen("output.txt" , "w" , stdout);

		int a , b;
		cin >> a >> b;
		for(int i =1;i<=a;i++)
		{
			scanf("%d",arr+i);
		}
		for(int i = b ; ;i++)
		{
			if(i == a+1)
				i = 1;

			if(arr[i] == 1){cout << i ;break;}
		}
		return 0;

	}


/*
    Author : harshit15
      Before submission
    - Fast IO
    - mx value
    - int overflow
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 998244353
#define mx 1000005
#define mx2 15
#define inf 4e18
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
main()
{

    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
    int n,k;
    cin >> n >> k;
    int arr[n];
    k = (k - 1 + n )%n;
    k %= n;

    fl(i,0,n)
    {
        cin >> arr[i];

    }
    int x = 2*n;
    while(x--)
    {
        if(arr[k] == 1)
        {
            cout<<k+1<<endl;
            return 0;
        }
        k++;
        k %= n;
    }

}


/*
    Author : harshit15
      Before submission
    - Fast IO
    - mx value
    - int overflow
*/
#include <bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mod 998244353
#define mx 1000005
#define mx2 15
#define inf 4e18
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
main()
{

    //freopen("input.txt" , "r" , stdin);
    //freopen("output.txt" , "w" , stdout);
    int n,k;
    cin >> n >> k;
    int arr[n];
    k = (k - 1 + n )%n;
    k %= n;

    fl(i,0,n)
    {
        cin >> arr[i];

    }
    int x = 2*n;
    while(x--)
    {
        if(arr[k] == 1)
        {
            cout<<k+1<<endl;
            return 0;
        }
        k++;
        k %= n;
    }

}


//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
    int n,k;
    cin>>n>>k;
    int a[n];
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int cnt=k;
    while(a[cnt]==0){
        cnt++;
        if(cnt>n){
            cnt=1;
        }
    }
    cout<<cnt<<endl;
    return 0;
}


//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
    int n,k,cnt=1;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int j=k-1;
    while(a[j]!=1){
        j--;
        cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}


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

int main()
{
    ll n,k;
    cin>>n>>k;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    while(a[k-1]==0)
    {
        k++;
        if(k==n+1)
        {
            k=1;
        }
    }
    cout<<k;
    return 0;
}


#include <bits/stdc++.h>
int n, k, a[1001];
using namespace std;
int main(){
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for(int i = 1; i <= n; i ++)
		scanf("%d", &a[i]);
	while(a[k] == 0)
		k = k % n + 1;
	cout<<k<<endl;
}


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

int main()
{
    ll n,k;
    cin>>n>>k;
    ll v[n];
    for(ll i=0;i<n;i++)
        cin>>v[i];
    while(v[k-1]==0)
    {
        k++;
        if(k==n+1)
        {
            k=1;
        }
    }
    cout<<k;
    return 0;
}


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

int main()
{
    ll n,k;
    cin>>n>>k;
    vector<ll> v;
    for(ll i=0;i<n;i++)
    {
        ll ip;
        cin>>ip;
        v.push_back(ip);
    }
    while(v[k-1]==0)
    {
        k++;
        if(k==n+1)
        {
            k=1;
        }
    }
    cout<<k;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<math.h>
#include<utility>
#include<algorithm>
using namespace std;
typedef  long long LL;
//const auto INF = 0x3f3f3f3f;
//const int N = 1e2+5;
int a[1005];
int main() {
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i=1; i<=n; i++) {
        scanf("%d", &a[i]);
    }
    while(a[k] == 0) {
        k++;
        if(k==n+1)
            k=1;
    }
    printf("%d\n", k);
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<math.h>
#include<utility>
#include<algorithm>
using namespace std;
typedef  long long LL;
//const auto INF = 0x3f3f3f3f;
//const int N = 1e2+5;
int a[1005];
int main() {
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i=1; i<=n; i++) {
        scanf("%d", &a[i]);
    }
    while(a[k] == 0) {
        k++;
        if(k==n+1)
            k=1;
    }
    printf("%d\n", k);
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<math.h>
#include<utility>
#include<algorithm>
using namespace std;
typedef  long long LL;
//const auto INF = 0x3f3f3f3f;
//const int N = 1e2+5;
int a[1005];
int main() {
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i=1; i<=n; i++) {
        scanf("%d", &a[i]);
    }
    while(a[k] == 0) {
        k++;
        if(k==n+1)
            k=1;
    }
    printf("%d\n", k);
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<math.h>
#include<utility>
#include<algorithm>
using namespace std;
typedef  long long LL;
//const auto INF = 0x3f3f3f3f;
//const int N = 1e2+5;
int a[1005];
int main() {
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i=1; i<=n; i++) {
        scanf("%d", &a[i]);
    }
    while(a[k] == 0) {
        k++;
        if(k==n+1)
            k=1;
    }
    printf("%d\n", k);
}


#include <iostream>
#include <stdio.h>
using namespace std;
int arr[1000];
int main()
{
    int n, m, res = 0;
    bool b = true;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> arr[i];
        if(i>=m&&arr[i]==1&&b)
        {
            res = i;
            b = false;
        }
    }
    if(res==0)for(int i=1; i<m; i++)
    {
        if(arr[i]==1&&b)
        {
            res = i;
            b = false;
        }
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
int arr[1000];
int main()
{
    int n, m, res = 0;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> arr[i];
        if(i>=m&&arr[i]==1)res = m;
    }
    if(res==0)for(int i=1; i<m; i++)
    {
        if(arr[i]==1)
        {
            res = i;
            break;
        }
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
bool arr[1000];
int main()
{
    int n, m, a, res = 0;
    ifstream inFile;
    ofstream outFile;
    inFile.open("input.txt");
    inFile >> n >> m;
    for(int i=0; i<n; i++)
    {
        cin >> a;
        if(a==1&&m>0)
        {
            res++;
            m--;
        }
    }
    outFile << res;
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
int arr[1000];
int main()
{
    int n, m, res = 0;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> arr[i];
        if(i>=m&&arr[i]==1)res = m;
    }
    if(res==0)for(int i=1; i<m; i++)if(arr[i]==1)res = i;
    cout << res;
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
bool arr[1000];
int main()
{
    int n, m, a, res = 0;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> m;
    for(int i=0; i<n; i++)
    {
        cin >> a;
        if(a==1&&m>0)
        {
            res++;
            m--;
        }
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <stdio.h>
using namespace std;
int arr[1000];
int main()
{
    int n, m, res = 0;
    bool b = true;
    /*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> arr[i];
        if(i>=m&&arr[i]==1&&b)
        {
            res = i;
            b = false;
        }
    }
    if(res==0)for(int i=1; i<m; i++)
    {
        if(arr[i]==1&&b)
        {
            res = i;
            b = false;
        }
    }
    cout << res;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n , k ; cin >> n >> k ;
    vector<int> arr(n);
    for(int&p:arr) cin >> p ;
    for(int i = k ; i < 2*n ; i++)
    {
        if(i>=n)
        {
            int num = i%n;
            if(arr[num]==1)
            {
                cout << num+1 << endl;
                return 0 ;
            }
        }
        else
        {
            if(arr[i]==1)
            {
                cout << i << endl;
                return 0 ;
            }
        }
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    int n , k ; cin >> n >> k ;
    vector<int> arr(n);
    for(int&p:arr) cin >> p ;
    for(int i = k-1 ; i < 2*n ; i++)
    {
        if(i>=n)
        {
            int num = i%n;
            if(arr[num]==1)
            {
                cout << num+1 << endl;
                return 0 ;
            }
        }
        else
        {
            if(arr[i]==1)
            {
                cout << i+1 << endl;
                return 0 ;
            }
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
const int MOD = 1e9+7;
using namespace std;

signed main()
{
     int t,n,k,m,l,xl,xxl,c=0,d;
     cin>>n>>k; int a[n];
     for(int i=0; i<n; i++)
     {
         cin>>a[i];
         c+=a[i];
     }
    cout<<min(c,k)<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
const int MOD = 1e9+7;
using namespace std;

signed main()
{
     int t,n,k,m,l,xl,xxl,c=0,d;
     ifstream("input.txt");
     ofstream("output.txt");
     cin>>n>>k; int a[n];
     for(int i=0; i<n; i++)
     {
         cin>>a[i];
         c+=a[i];
     }
    cout<<min(c,k)<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
const int MOD = 1e9+7;
using namespace std;

signed main()
{
     int t,n,k,m,l,xl,xxl,c=0,d;
     ifstream cin("input.txt");
     ofstream cout("output.txt");
     cin>>n>>k; int a[n];
     for(int i=0; i<n; i++)
     {
         cin>>a[i];
     }
     if(a[k-1]==1){
        cout<<k<<endl;
     }
     else{
        for(int i=k-1; i<n; i++){
            if(a[i]==1){
                cout<<i+1<<endl;
                break;
            }
        }
     }

    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
const int MOD = 1e9+7;
using namespace std;

signed main()
{
     int t,n,k,m,l,xl,xxl,c=0,d;
     ifstream cin("input.txt");
     ofstream cout("output.txt");
     cin>>n>>k; int a[n];
     for(int i=0; i<n; i++)
     {
         cin>>a[i];
         c+=a[i];
     }
    cout<<min(c,k)<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
const int MOD = 1e9+7;
using namespace std;

signed main()
{
     int t,n,k,m,l,xl,xxl,c=0,d;
     ifstream cin("input.txt");
     ofstream cout("output.txt");
     cin>>n>>k; int a[n];
     for(int i=0; i<n; i++)
     {
         cin>>a[i];
     }
     if(a[k-1]==1){
        cout<<k<<endl;
     }
     else{
        for(int i=k-1; i<n+k-1; (i++)%n){
            if(a[i%n]==1){
                    if((i+1)%n==0)
                    cout<<n<<endl;
            else
                cout<<(i+1)%n<<endl;
                break;
            }
        }
     }

    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#define ll long long
const int MOD = 1e9+7;
using namespace std;

signed main()
{
     int t,n,k,m,l,xl,xxl,c=0,d;
     ifstream cin("input.txt");
     ofstream cout("output.txt");
     cin>>n>>k; int a[n];
     for(int i=0; i<n; i++)
     {
         cin>>a[i];
     }
     if(a[k-1]==1){
        cout<<k<<endl;
     }
     else{
        for(int i=k-1; i<n+k-1; (i++)%n){
            if(a[i%n]==1){
                cout<<(i+1)%n<<endl;
                break;
            }
        }
     }

    return 0;
}


/* Alisher Rakhimberdi  */
/*    Hala Madrid      */
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <deque>
#include <ctime>
#include <stack>
#include <set>
#include <map>

#define f first
#define s second
#define ppb pop_back
#define pb push_back
#define mp make_pair
#define ll long long
#define int long long
#define ld long double
#define ull unsigned long long
#define sr(x) sort(x.begin(), x.end())
#define rv(x) reverse(x.begin(), x.end())
#define speed ios_base::sync_with_stdio(false)

using namespace std;

const int mod = 1e9 + 7;
const ld INF = 1e9 + 123;
const int inf = 1e9 + 123;
const int maxn = 1e5 + 123;
const ld pi = 3.141592653589793238462643;

int n, k;
int a[maxn];
vector < int > v;
int mn = inf;

main() {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		if (a[i] == 1) {
			v.pb(i);
		}
	}
	if (a[k] == 1) {
		cout << k;
		return 0;
	} else {
		for (int i = 0; i < v.size(); ++i) {
			mn = min(mn, abs(k - v[i]));
		}
		cout << mn + 1;
		return 0;
	}
	return 0;
}


/* Alisher Rakhimberdi  */
/*    Hala Madrid      */
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <deque>
#include <ctime>
#include <stack>
#include <set>
#include <map>

#define f first
#define s second
#define ppb pop_back
#define pb push_back
#define mp make_pair
#define ll long long
#define int long long
#define ld long double
#define ull unsigned long long
#define sr(x) sort(x.begin(), x.end())
#define rv(x) reverse(x.begin(), x.end())
#define speed ios_base::sync_with_stdio(false)

using namespace std;

const int mod = 1e9 + 7;
const ld INF = 1e9 + 123;
const int inf = 1e9 + 123;
const int maxn = 1e5 + 123;
const ld pi = 3.141592653589793238462643;

int n, k;
int a[maxn];

main() {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	int i = 1;
	while (true) {
		if (a[k] == 1) {
			cout << k;
			return 0;
		} else {
			if (k == n) {
				for (int i = 1; i <= n; ++i) {
					if (a[i] == 1) {
						cout << i;
						return 0;
					}
				}
			} else {
				for (int i = k + 1; i <= n; ++i) {
					if (a[i] == 1) {
						cout << i;
						return 0;
					}
				}
			}
		}
	}
	return 0;
}


/* Alisher Rakhimberdi  */
/*    Hala Madrid      */
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <deque>
#include <ctime>
#include <stack>
#include <set>
#include <map>

#define f first
#define s second
#define ppb pop_back
#define pb push_back
#define mp make_pair
#define ll long long
#define int long long
#define ld long double
#define ull unsigned long long
#define sr(x) sort(x.begin(), x.end())
#define rv(x) reverse(x.begin(), x.end())
#define speed ios_base::sync_with_stdio(false)

using namespace std;

const int mod = 1e9 + 7;
const ld INF = 1e9 + 123;
const int inf = 1e9 + 123;
const int maxn = 1e5 + 123;
const ld pi = 3.141592653589793238462643;

int n, k;
int a[maxn];

main() {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	int i = 1;
	while (true) {
		if (a[k] == 1) {
			cout << k;
			return 0;
		} else {
			if (k == n) {
				k = 1;
			} else {
				k++;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define int ll
#define mod 1000000007
#define pii pair<ll,ll>
#define vi vector<ll>
#define mii map<ll,ll>
#define pb push_back
#define mp make_pair
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) ll x; cin>>x; while(x--)
#define endl '\n'
#define str string
void kehsihba()
{
   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
}
int32_t main() {
	kehsihba();
	ll n,k;cin>>n>>k;
	ll arr[n];
	for(int i=0;i<n;++i){
		cin>>arr[i];
	}
	k=k-1;
	while(arr[k]!=1){
		k=(k+1)%n;
	}
	cout<<k+1;
   	return 0;


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define int ll
#define mod 1000000007
#define pii pair<ll,ll>
#define vi vector<ll>
#define mii map<ll,ll>
#define pb push_back
#define mp make_pair
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) ll x; cin>>x; while(x--)
#define endl '\n'
#define str string
void kehsihba()
{
   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
}
int32_t main() {
	kehsihba();
	ll n,k;cin>>n>>k;
	ll arr[n];
	for(int i=0;i<n;++i){
		cin>>arr[i];
	}
	k=k-1;
	while(arr[k]!=1){
		k=(k+1)%n;
	}
	cout<<k+1;
   	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define int ll
#define mod 1000000007
#define pii pair<ll,ll>
#define vi vector<ll>
#define mii map<ll,ll>
#define pb push_back
#define mp make_pair
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) ll x; cin>>x; while(x--)
#define endl '\n'
#define str string
void kehsihba()
{
   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
}
int32_t main() {
	kehsihba();
	ll n,k;cin>>n>>k;
	ll x;
	vi ans;
	for(int i=0;i<n;++i){
		cin>>x;
		if(x==1){
			ans.pb(i+1);
		}
	}
	n=ans.size();
	ll i=0;
	ll as;
	while(k--){
		as=ans[(i++)%n];
	}
	cout<<as;
   	return 0;
}


// In The Name Of Allah

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

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int maxx = 1e3 + 19;

int n, k;
int a[maxx];

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i], a[i + n] = a[i];
    for (int i = k; i <= 2 * n; i++)
        if (a[i] == 1) {
            int x = i % n;
            if (x == 0)
                x = n;
            cout << x;
            break;
        }
    return 0;
}


// In The Name Of Allah

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

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int maxx = 2e3 + 19;

int n, k;
int a[maxx];

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i], a[i + n] = a[i];
    for (int i = k; i <= 2 * n; i++)
        if (a[i] == 1) {
            int x = i % n;
            if (x == 0)
                x = n;
            cout << x;
            break;
        }
    return 0;
}


// In The Name Of Allah

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

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int maxx = 1e3 + 19;

int n, k;
int a[maxx];

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i], a[i + n] = a[i];
    for (int i = k; i <= 2 * n; i++)
        if (a[i] == 1) {
            cout << i % n;
            break;
        }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define OP ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
typedef long long ll;

const int inf = int(1e9);
const int mod = inf + 7;
const double PI = acos(-1.0);
using namespace std;
const int N = 1e5 + 155;

void solve(){
    int n, k;
    cin >> n >> k;
    int a[n];
    rep(i, n) cin >> a[i];
    k--;
    while(!a[k]) {
        k++;
        k %= n;
    }
    cout << ++k;
}

int main()
{
    OP
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int t = 1;
    while(t--)
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define OP ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
typedef long long ll;

const int inf = int(1e9);
const int mod = inf + 7;
const double PI = acos(-1.0);
using namespace std;
const int N = 1e5 + 155;

void solve(){
    int n, k;
    cin >> n >> k;
    int a[n];
    rep(i, n) cin >> a[i];
    k--;
    while(!a[k]) {
        k++;
        k %= n;
    }
    cout << k;
}

int main()
{
    OP
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int t = 1;
    while(t--)
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,k,i;
  cin>>n>>k;
  int a[n+1];
  for(i=1;i<n+1;i++)
  cin>>a[i];
 for(i=1;i<=n;i++)
 {
     if(a[(i+k)%n]==1)
     break;
 }
 cout<<(i+k)%n<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  int n,k,i;
  cin>>n>>k;
  int a[n+1];
  for(i=1;i<n+1;i++)
  cin>>a[i];
 for(i=1;i<=n;i++)
 {
     if(a[(i+k)%n]==1)
     break;
 }
 cout<<(i+k)%n<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  long long int n,k,i;
  cin>>n>>k;
  long long int a[n];
  for(i=0;i<n;i++)
  cin>>a[i];
 for(i=0;i<n;i++)
 {
     if(a[(i+k-1)%n]==1)
     break;
 }
 cout<<(i+k-1)%n+1<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define fo(i,a,b) for(ll i=a;i<b;i++)
#define pi 3.141592653589793238
#define ld long double
int main()
{
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    int n,k;
    cin>>n>>k;
    int arr[n];
    fo(i,0,n)
    cin>>arr[i];
    if(arr[k-1]==1)
    {
        cout<<k;
        return 0;
    }
    else
    {
        int i=k;
        if(i>=n)
            i=i-n;
        while(arr[i]!=1)
        {
            i++;
            if(i>=n)
                i=i-n;
        }
        cout<<i+1;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define fo(i,a,b) for(ll i=a;i<b;i++)
#define pi 3.141592653589793238
#define ld long double
int main()
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    fo(i,0,n)
    cin>>arr[i];
    if(arr[k-1]==1)
    {
        cout<<k;
        return 0;
    }
    else
    {
        int i=k;
        if(i>=n)
            i=i-n;
        while(arr[i]!=1)
        {
            i++;
            if(i>=n)
                i=i-n;
        }
        cout<<i+1;
    }
    return 0;
}


                        //CREATED BY TARANG KAPOOR

#include <iostream>

using namespace std;

int main()
{
  int n,k; cin>>n>>k;
  int count=0,t,next_count=0,j;
  int a[n+1];
  for(int i=1 ; i<=n ; i++)
   cin>>a[i];
   if(a[k]==1)
   cout<<k<<endl;
   else
   {
       for(int i=k+1 ; i<=n ; i++){
          if(a[i]==1){
              t=i;
              count++;
              break;
          }
       }
       if(count==1)
       cout<<t<<endl;
       else
       {
           for(int i=1 ; i<=k-1 ; i++){
               if(a[i]==1)
               {
                   j=i;
                   break;
               }
           }
          cout<<j<<endl;
       }
   }

}


                             //CREATED BY TARANG KAPOOR

#include <iostream>

using namespace std;

int main()
{
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
  int n,k; cin>>n>>k;
  int count=0,t,next_count=0,j;
  int a[n+1];
  for(int i=1 ; i<=n ; i++)
   cin>>a[i];
   if(a[k]==1)
   cout<<k<<endl;
   else
   {
       for(int i=k+1 ; i<=n ; i++){
          if(a[i]==1){
              t=i;
              count++;
              break;
          }
       }
       if(count==1)
       cout<<t<<endl;
       else
       {
           for(int i=1 ; i<=k-1 ; i++){
               if(a[i]==1)
               {
                   j=i;
                   break;
               }
           }
          cout<<j<<endl;
       }
   }

}


#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n,k;
    int h;
    cin >> n >> k;
    int m[n];
    for(int i = 0; i < n; i++)
    {
        cin >> m[i];
    }
    for(int i = k - 1; i < n; i++)
    {
        if(m[i] == 1)
        {
            h = i + 1;
            break;
        }
        if(i = n - 1)
        {
            i = 0;
        }

    }
    cout << h;
    return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n,k;
    int h;
    cin >> n >> k;
    int m[n];
    for(int i = 0; i < n; i++)
    {
        cin >> m[i];
    }
    for(int i = k - 1; i < n; i++)
    {
        if(m[i] == 1)
        {
            h = i + 1;
            break;
        }
        if(i = n - 1)
        {
            i = -1;
        }

    }
    cout << h;
    return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n,k;
    int h;
    cin >> n >> k;
    int m[n];
    for(int i = 0; i < n; i++)
    {
        cin >> m[i];
    }
    for(int i = k - 1; i < n; i++)
    {
        if(m[i] == 1)
        {
            h = i + 1;
            break;
        }
        if(i == n - 1)
        {
            i = -1;
        }

    }
    cout << h;
    return 0;
}


   #include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;

   int *x;
   x=new int[n];

   for(int i=0;i<n;i++)
    cin>>x[i];
   int c=-1;
   for(int i=k-1;i<n;i++)
   {
       if(x[i]==1)
       {
           c=i+1;
           break;
       }
   }

   if(c>-1)
   {
       cout<<c<<endl;
   }
   else
   {
    for(int i=0;i<k;i++)
   {
       if(x[i]==1)
       {
           c=i+1;
           break;
       }

   }
   cout<<c<<endl;
   }

    return 0;
}


   #include <bits/stdc++.h>

using namespace std;

int main()
{
    freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    int n,k;
    cin>>n>>k;

   int *x;
   x=new int[n];

   for(int i=0;i<n;i++)
    cin>>x[i];
   int c=-1;
   for(int i=k-1;i<n;i++)
   {
       if(x[i]==1)
       {
           c=i+1;
           break;
       }
   }

   if(c>-1)
   {
       cout<<c<<endl;
   }
   else
   {
    for(int i=0;i<k;i++)
   {
       if(x[i]==1)
       {
           c=i+1;
           break;
       }

   }
   cout<<c<<endl;
   }

    return 0;
}


#include<bits/stdc++.h>
#define IRONMAN ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main() {
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	IRONMAN

	int n,k,p=0;cin>>n>>k;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin>>a[i];
    for(int i=0; i<n; i++) {
		if(a[k-1]==1) {p=k;break;}
		else {
			if(k==n) {
				for(int j=0;j<n; j++) {
					if(a[j]==1) {p=j+1;break;}
				}
			}else {
			    bool ok=false;
				for(int j=k; j<n; j++) {
					if(a[j]==1) {p=j+1;ok=true;break;}
				}
				if(!ok){
				   for(int j=0;j<n; j++) {
					   if(a[j]==1) {p=j+1;break;}
				   }
				}
			}
		}
	}
	cout<<p<<endl;
}


#include<bits/stdc++.h>
#define IRONMAN ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main() {
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	IRONMAN

	int n,k;cin>>n>>k;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin>>a[i];
	if(a[k-1]==1) cout<<k<<endl;
    else {
			++k;
			if(k>=n) k=1;
			while(k<n) {
				if(a[k-1]==1) {cout<<k;return 0;}
				else ++k;
				if(k==n) k=0;
			}
		}

}


#include<bits/stdc++.h>
#define IRONMAN ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main() {
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	IRONMAN

	int n,k,p=0;cin>>n>>k;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin>>a[i];
    for(int i=0; i<n; i++) {
		if(a[k-1]==1) {p=k;break;}
		else {
			if(k==n) {
				for(int j=0;j<n; j++) {
					if(a[j]==1) {p=j+1;break;}
				}
			}else {
				for(int j=k; j<n; j++) {
					if(a[j]==1) {p=j+1;break;}
				}
			}
		}
	}
	cout<<p<<endl;
}


#include<bits/stdc++.h>
#define IRONMAN ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main() {
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	IRONMAN

	int n,k;cin>>n>>k;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin>>a[i];
	if(a[k-1]==1) cout<<k<<endl;
    else {
			++k;
			if(k>=n) k=0;
			while(k<n) {
				if(a[k]==1) {cout<<k+1;return 0;}
				else ++k;
				if(k==n) k=0;
			}
		}

}


#include<bits/stdc++.h>
#define IRONMAN ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main() {
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	IRONMAN

	int n,k;cin>>n>>k;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin>>a[i];
	k=k-1;
	while(a[k]!=1){
	    ++k;
	    k%=n;
	}
    cout<<k+1<<endl;
}


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

//#define cin fin
//ifstream fin ("input.txt");
//#define cout fout
//ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < n; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i) % n;
        cout << number << endl;
        if(works[number])
        {
            cout << number+1 << endl;
//            fout.close();
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

#define cin fin
ifstream fin ("input.txt");
#define cout fout
ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < n; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i) % n;
        if(works[number])
        {
            cout << number+1 << endl;
//            fout.close();
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

#define cin fin
ifstream fin ("input.txt");
#define cout fout
ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < k; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i) % n;
        if(works[number])
        {
            cout << number+1 << endl;
            fout.close();
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

#define cin fin
ifstream fin ("input.txt");
#define cout fout
ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < k; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i) % n;
        if(works[number])
        {
            cout << number+1 << endl;
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

#define cin fin
ifstream fin ("input.txt");
#define cout fout
ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < k; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i+1) % n;
        if(works[number-1])
        {
            cout << number << endl;
//            fout.close();
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

//#define cin fin
//ifstream fin ("input.txt");
//#define cout fout
//ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < k; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i) % n;
        if(works[number])
        {
            cout << number+1 << endl;
//            fout.close();
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


/*
ID: dzhouwave
PROG: 120B
LANG: C++11
*/

// @BEGIN_OF_SOURCE_CODE

#include <bits/stdc++.h>

#define pi acos(-1.0)
#define LL unsigned long long
using namespace std;

#define cin fin
ifstream fin ("input.txt");
#define cout fout
ofstream fout ("output.txt");

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> works;
    for(int i = 0; i < n; i++)
    {
        bool input;
        cin >> input;
        works.push_back(input);
    }
    k--;
    for(int i = 0; i < n; i++)
    {
        int number = (k+i) % n;
        cout << number << endl;
        if(works[number])
        {
            cout << number+1 << endl;
//            fout.close();
            return 0;
        }
    }
    return 0;
}

// @END_OF_SOURCE_CODE


# include <iostream>
using namespace std;
int main(){
  freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
  int n,j,i,k;
  cin>>n>>k;
  int ar[n]={0};
  for(i=1;i<=n;i++){
    cin>>ar[i];
  }
    if(ar[k-1]==0){
      for(j=k-1;j<n;j++){
        if(ar[j]>0){
          cout<<j+1;
          break;
        }
        if(j==n-1){
          j=0;
        }
      }
    }
    else{
      cout<<k;
    }
  return 0;
}


# include <iostream>
using namespace std;
int main(){
  long long int n, j,i,k;
  cin>>n>>k;
  int ar[n+1];
  for(i=1;i<=n;i++){
    cin>>ar[i];
  }
    if(ar[k]==0){
      if(k==n){
        i=1;
      }
      for(j=i;j<=n;j++){
        if(ar[j]>0){
          cout<<j;
          break;
        }
      }
    }
    else{
      cout<<k;
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target ("avx2,avx,fma")
#define ll long long int
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(i=l;i<r;i++)
#define bep(i,l,r) for(i=l;i>=r;i--)
#define all(v) v.begin(),v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int,int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
int main()
{
    io;
    int i,j,n;
    cin>>n;
    int a;
    unordered_map<int,int>p;
    vector<int>v;
    rep(i,0,n)
    {
    cin>>a;
    p[a]++;
    }
    for(auto e:p)
    {
       v.pb(e.ss);
    }
    sort(v.begin(),v.end());
    vector<int>m(n+1);
    int ans=0;
    int na,nb;
    rep(i,1,n+1)
    {
        int l=lower_bound(v.begin(),v.end(),i)-v.begin();
        m[i]=m[i-1]+((int)v.size()-l);
        int b=m[i]/i;
        if(i<=b&&i*b>ans)
        {
            ans=i*b;
            na=i;
            nb=b;
        }
    }
    vector<pii>vv;
    for(auto e:p)
    {
        if(e.ss>na)
            vv.pb(mp(na,e.ff));
        else
            vv.pb(mp(e.ss,e.ff));
    }
    sort(vv.begin(),vv.end());
    int mat[na][nb];
    memset(mat,0,sizeof(mat));
    i=0;
    j=0;
    rep(a,0,vv.size())
    {
        mat[i][j]=vv[a].ss;
        vv[a].ff--;
        if(vv[a].ff)
            a--;
        i=(i+1)%na;
        j=(j+1)%nb;
        if(mat[i][j])
            i=(i+1)%na;
    }
    cout<<ans<<"\n";
    cout<<na<<" "<<nb<<"\n";
    rep(i,0,na)
    {
    rep(j,0,nb)
        cout<<mat[i][j]<<" ";
    cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target ("avx2,avx,fma")
#define ll long long int
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(i=l;i<r;i++)
#define bep(i,l,r) for(i=l;i>=r;i--)
#define all(v) v.begin(),v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int,int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
int main()
{
    io;
    int i,j,n;
    cin>>n;
    int a;
    unordered_map<int,int>p;
    vector<int>v;
    rep(i,0,n)
    {
    cin>>a;
    p[a]++;
    }
    for(auto e:p)
    {
       v.pb(e.ss);
    }
    sort(v.begin(),v.end());
    vector<int>m(n+1);
    int ans=0;
    int na,nb;
    rep(i,1,n+1)
    {
        int l=lower_bound(v.begin(),v.end(),i)-v.begin();
        m[i]=m[i-1]+((int)v.size()-l);
        int b=m[i]/i;
        if(i<=b&&i*b>ans)
        {
            ans=i*b;
            na=i;
            nb=b;
        }
    }
    vector<pii>vv;
    for(auto e:p)
    {
        if(e.ss>na)
            vv.pb(mp(na,e.ff));
        else
            vv.pb(mp(e.ss,e.ff));
    }
    sort(vv.begin(),vv.end(),greater<pii>() );
    int mat[na][nb];
    memset(mat,0,sizeof(mat));
    i=0;
    j=0;
    rep(a,0,vv.size())
    {
        mat[i][j]=vv[a].ss;
        vv[a].ff--;
        if(vv[a].ff)
            a--;
        i=(i+1)%na;
        j=(j+1)%nb;
        if(mat[i][j])
            i=(i+1)%na;
    }
    cout<<ans<<"\n";
    cout<<na<<" "<<nb<<"\n";
    rep(i,0,na)
    {
    rep(j,0,nb)
        cout<<mat[i][j]<<" ";
    cout<<"\n";
    }
    return 0;
}


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

#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()

using ll = long long;
using ld = long double;

ll getBest(ll x) {
    ll res = 1;
    for (ll i = 2; i*i <= x; i++) {
        if (x % i == 0) res = i;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    map<ll, ll> count;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        count[x]++;
    }
    priority_queue<pair<ll, ll>> pq;
    for (auto e : count) {
        pq.push({e.second, e.first});
    }
    for (ll i = n;; i--) {
        ll a = getBest(i);
        ll b = i / a;
        if (pq.top().first <= a) {
            cout << i << endl;
            cout << a << " " << b << endl;
            vector<ll> values;
            while (!pq.empty()) {
                auto e = pq.top();
                pq.pop();
                for (ll j = 0; j < e.first; j++) {
                    values.push_back(e.second);
                }
            }
            for (ll j = 0; j < a; j++) {
                for (ll k = 0; k < b; k++) {
                    ll p = j + ((k + j) % b) * a;
                    cout << values[p] << " ";
                }
                cout << endl;
            }
            return 0;
        }
        auto e = pq.top();
        pq.pop();
        e.first--;
        if (e.first > 0) pq.push(e);
    }
}


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

#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()

using ll = long long;
using ld = long double;

ll getBest(ll x) {
    ll res = 1;
    for (ll i = 2; i*i <= x; i++) {
        if (x % i == 0) res = i;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    map<ll, ll> count;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        count[x]++;
    }
    priority_queue<pair<ll, ll>> pq;
    for (auto e : count) {
        pq.push({e.second, e.first});
    }
    for (ll i = n;; i--) {
        ll a = getBest(i);
        ll b = i / a;
        if (pq.top().first <= a) {
            cout << i << endl;
            cout << a << " " << b << endl;
            vector<ll> values;
            while (!pq.empty()) {
                auto e = pq.top();
                pq.pop();
                for (ll j = 0; j < e.first; j++) {
                    values.push_back(e.second);
                }
            }
            for (ll j = 0; j < a; j++) {
                for (ll k = 0; k < b; k++) {
                    ll p = j + ((k - j + b) % b) * a;
                    cout << values[p] << " ";
                }
                cout << endl;
            }
            return 0;
        }
        auto e = pq.top();
        pq.pop();
        e.first--;
        if (e.first > 0) pq.push(e);
    }
}


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

#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()

using ll = long long;
using ld = long double;

ll getBest(ll x) {
    for (ll i = sqrt(x); i > 1; i--) {
        if (x % i == 0) return i;
    }
    return 1;
    ll res = 1;
    for (ll i = 2; i*i <= x; i++) {
        if (x % i == 0) res = i;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    map<ll, ll> count;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        count[x]++;
    }
    priority_queue<pair<ll, ll>> pq;
    for (auto e : count) {
        pq.push({e.second, e.first});
    }
    for (ll i = n;; i--) {
        ll a = getBest(i);
        ll b = i / a;
        if (pq.top().first <= a) {
            cout << i << endl;
            cout << a << " " << b << endl;
            vector<ll> values;
            while (!pq.empty()) {
                auto e = pq.top();
                pq.pop();
                for (ll j = 0; j < e.first; j++) {
                    values.push_back(e.second);
                }
            }
            for (ll j = 0; j < a; j++) {
                for (ll k = 0; k < b; k++) {
                    ll p = j + ((k - j + b) % b) * a;
                    cout << values[p] << " ";
                }
                cout << "\n";
            }
            return 0;
        }
        auto e = pq.top();
        pq.pop();
        e.first--;
        if (e.first > 0) pq.push(e);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
typedef pair<int, int> P;
typedef pair<int, P> PPI;
typedef pair<LL, LL> PL;
typedef long long LL;
// LLONG_MIN, INT_MIN
#define xx first
#define yy second

int main()
{
  //freopen("input", "r", stdin);
  // freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin>>n;
  unordered_map<int,int> mp;
  for(int i=0;i<n;i++){
    int t;cin>>t;
    mp[t]++;
  }
  vector<P> da;
  for(auto p: mp)
    da.push_back({p.yy, p.xx});
  sort(da.begin(), da.end());
  // reverse(da.begin(), da.end());
  vector<int> psm(n+1);
  int mx=0, x=0,y=0;
  for(int i=0;i<da.size();i++)
    psm[i+1] = psm[i] + da[i].xx;
  for(int i=1;i<=da.back().yy;i++){
    int idx = lower_bound(da.begin(), da.end(), P({i, -1})) - da.begin();
    int tt = psm[idx] + (da.size() - idx) * i;
    if((tt/i) * i>mx){
      mx = (tt/i) * i;
      x = i;
      y = (tt/i);
    }
  }
  int ans[x][y];
  vector<int> use;
  for(int i=da.size()-1;i>=0;i--){
    for(int j=min(x, da[i].xx);j>0;j--)
      use.push_back(da[i].yy);
  }
  int now = 0;
  for(int i=0;i<y;i++){
    for(int j=0;j<x;j++)
      ans[j][(i+j)%y] = use[now++];
  }
  cout<<x*y<<endl;
  cout<<x<<" "<<y<<endl;
  for(int i=0;i<x;i++){
    for(int j=0;j<y;j++)
      cout<<ans[i][j]<<" ";
    cout<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
typedef pair<int, int> P;
typedef pair<int, P> PPI;
typedef pair<LL, LL> PL;
typedef long long LL;
// LLONG_MIN, INT_MIN
#define xx first
#define yy second

int main()
{
  //freopen("input", "r", stdin);
  // freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin>>n;
  unordered_map<int,int> mp;
  for(int i=0;i<n;i++){
    int t;cin>>t;
    mp[t]++;
  }
  vector<P> da;
  for(auto p: mp)
    da.push_back({p.yy, p.xx});
  sort(da.begin(), da.end());
  // reverse(da.begin(), da.end());
  vector<int> psm(n+1);
  int mx=0, x=0,y=0;
  for(int i=0;i<da.size();i++)
    psm[i+1] = psm[i] + da[i].xx;

  for(int i=1;i<=n;i++){
    int idx = lower_bound(da.begin(), da.end(), P({i, -1})) - da.begin();
    int tt = psm[idx] + (da.size() - idx) * i;
    if(tt/i >= i && (tt/i) * i>mx){
      mx = (tt/i) * i;
      x = i;
      y = (tt/i);
    }
  }
  int ans[x][y];
  vector<int> use;
  for(int i=da.size()-1;i>=0;i--){
    for(int j=min(x, da[i].xx);j>0;j--)
      use.push_back(da[i].yy);
  }
  int now = 0;
  for(int i=0;i<y;i++){
    for(int j=0;j<x;j++)
      ans[j][(i+j)%y] = use[now++];
  }
  cout<<x*y<<endl;
  cout<<x<<" "<<y<<endl;
  for(int i=0;i<x;i++){
    for(int j=0;j<y;j++)
      cout<<ans[i][j]<<" ";
    cout<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
typedef pair<int, int> P;
typedef pair<int, P> PPI;
typedef pair<LL, LL> PL;
typedef long long LL;
// LLONG_MIN, INT_MIN
#define xx first
#define yy second

int main()

  //freopen("input", "r", stdin);
  //freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin>>n;
  unordered_map<int,int> mp;
  for(int i=0;i<n;i++){
    int t;cin>>t;
    mp[t]++;
  }
  vector<P> da;
  for(auto p: mp)
    da.push_back({p.yy, p.xx});
  sort(da.begin(), da.end());
  // reverse(da.begin(), da.end());
  vector<int> psm(n+1);
  int mx=0, x=0,y=0;
  for(int i=0;i<da.size();i++)
    psm[i+1] = psm[i] + da[i].xx;
  for(int i=1;i<=da.back().xx;i++){
    int idx = lower_bound(da.begin(), da.end(), P({i, -1})) - da.begin();
    int tt = psm[idx] + (da.size() - idx) * i;
    if((tt/i) * i>mx){
      mx = (tt/i) * i;
      x = i;
      y = (tt/i);
    }
  }
  int ans[x][y];
  vector<int> use;
  for(int i=da.size()-1;i>=0;i--){
    for(int j=min(x, da[i].xx);j>0;j--)
      use.push_back(da[i].yy);
  }
  int now = 0;
  for(int i=0;i<y;i++){
    for(int j=0;j<x;j++)
      ans[j][(i+j)%y] = use[now++];
  }
  cout<<x*y<<endl;
  cout<<x<<" "<<y<<endl;
  for(int i=0;i<x;i++){
    for(int j=0;j<y;j++)
      cout<<ans[i][j]<<" ";
    cout<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
typedef pair<int, int> P;
typedef pair<int, P> PPI;
typedef pair<LL, LL> PL;
typedef long long LL;
// LLONG_MIN, INT_MIN
#define xx first
#define yy second

int main()
{
  //freopen("input", "r", stdin);
  // freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin>>n;
  unordered_map<int,int> mp;
  for(int i=0;i<n;i++){
    int t;cin>>t;
    mp[t]++;
  }
  vector<P> da;
  for(auto p: mp)
    da.push_back({p.yy, p.xx});
  sort(da.begin(), da.end());
  // reverse(da.begin(), da.end());
  vector<int> psm(n+1);
  int mx=0, x=0,y=0;
  for(int i=0;i<da.size();i++)
    psm[i+1] = psm[i] + da[i].xx;
  for(int i=1;i<=da.back().yy;i++){
    int idx = lower_bound(da.begin(), da.end(), P({i, -1})) - da.begin();
    int tt = psm[idx] + (da.size() - idx) * i;
    if((tt/i) * i>mx){
      mx = (tt/i) * i;
      x = i;
      y = (tt/i);
    }
  }
  int ans[x][y];
  vector<int> use;
  for(int i=da.size()-1;i>=0;i--){
    for(int j=min(x, da[i].xx);j>0;j--)
      use.push_back(da[i].yy);
  }
  // cout<<use.size()<<endl;
  // for(auto i:use)
  //   cout<<i<<" ";
  // cout<<endl;
  int now = 0;
  for(int i=0;i<y;i++){
    for(int j=0;j<x;j++)
      ans[j][(i+j)%y] = use[now++];
  }
  cout<<x*y<<endl;
  cout<<x<<" "<<y<<endl;
  for(int i=0;i<x;i++){
    for(int j=0;j<y;j++)
      cout<<ans[i][j]<<" ";
    cout<<endl;
  }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
typedef pair<int, int> P;
typedef pair<int, P> PPI;
typedef pair<LL, LL> PL;
typedef long long LL;
// LLONG_MIN, INT_MIN
#define xx first
#define yy second

int main()
{
  //freopen("input", "r", stdin);
  // freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin>>n;
  unordered_map<int,int> mp;
  for(int i=0;i<n;i++){
    int t;cin>>t;
    mp[t]++;
  }
  vector<P> da;
  for(auto p: mp)
    da.push_back({p.yy, p.xx});
  sort(da.begin(), da.end());
  // reverse(da.begin(), da.end());
  vector<int> psm(n+1);
  int mx=0, x=0,y=0;
  for(int i=0;i<da.size();i++)
    psm[i+1] = psm[i] + da[i].xx;

  for(int i=1;i<=da.back().xx;i++){
    int idx = lower_bound(da.begin(), da.end(), P({i, -1})) - da.begin();
    int tt = psm[idx] + (da.size() - idx) * i;
    if(tt/i >= i && (tt/i) * i>mx){
      mx = (tt/i) * i;
      x = i;
      y = (tt/i);
    }
  }
  int ans[x][y];
  vector<int> use;
  for(int i=da.size()-1;i>=0;i--){
    for(int j=min(x, da[i].xx);j>0;j--)
      use.push_back(da[i].yy);
  }
  int now = 0;
  for(int i=0;i<y;i++){
    for(int j=0;j<x;j++)
      ans[j][(i+j)%y] = use[now++];
  }
  cout<<x*y<<endl;
  cout<<x<<" "<<y<<endl;
  for(int i=0;i<x;i++){
    for(int j=0;j<y;j++)
      cout<<ans[i][j]<<" ";
    cout<<endl;
  }
}


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

#define IOS                               \
	ios_base::sync_with_stdio(false); \
	cin.tie(0);                       \
	cout.tie(0);                      \
	cin.exceptions(cin.failbit);
// #define int long long int //Comment when Unnecessary
#define ll long long
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define pii pair<int, int>
#define vi vector<int>
#define F first
#define S second
#define endl "\n" // Remove on Interactive

const int N = 300009;
const int mod = 1000000007;

int n, x;
map<int, int> mp;
vector<pii> zi;
vector<int> z;
vector<int> see;
vector<int> pre;

int32_t main()
{
	IOS;
	cin >> n;
	rep(i, 0, n)
	{
		cin >> x;
		mp[x]++;
	}
	trav(it, mp)
	{
		z.push_back(it.S);
		zi.push_back({it.S, it.F});
	}
	sort(all(zi));
	rep(i, 0, sz(zi))
	{
		rep(j, 0, zi[i].F)
		{
			see.push_back(zi[i].S);
		}
	}
	sort(all(z));
	pre.push_back(0);
	rep(i, 0, sz(z))
	{
		pre.push_back(pre[i] + z[i]);
	}
	int mx = 0;
	int ansi, ansj;
	rep(i, 1, n + 1)
	{
		int take = 0;
		auto it = lower_bound(all(z), i);
		int itne = it - z.begin();
		int sum = 0;
		if (itne > 0)
			sum += pre[itne];
		int bade = sz(z) - itne;
		int le = sum + bade * i;
		int mxj = le / i;
		mxj = min((n / i), mxj);
		if (mxj < i)
			continue;
		if (mx < (mxj * i))
		{
			ansi = i, ansj = mxj;
			mx = (mxj * i);
		}
	}
	// cout << ansi << " " << ansj << endl;
	// trav(it, see)
	// {
	// 	cout << it << " ";
	// }
	// cout << endl;
	int put[ansi][ansj];
	int row = 0;
	int col = 0;
	int prevr = 0, prevc = 0;
	mp.clear();
	vi see1;
	rep(i, 0, n)
	{
		if (mp[see[i]] == ansi)
			continue;
		see1.push_back(see[i]);
		mp[see[i]]++;
	}
	n = sz(see1);
	rep(i, (n - (ansi * ansj)), n)
	{
		put[row % ansi][col % ansj] = see1[i];
		row += 1;
		col += 1;
		if ((row % ansi) == prevr && (col % ansj) == prevc)
		{
			row += 1;
			prevr = (row % ansi);
			prevc = (col % ansj);
		}
	}
	cout << mx << endl;
	cout << ansi << " " << ansj << endl;
	rep(i, 0, ansi)
	{
		rep(j, 0, ansj)
		{
			cout << put[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}


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

#define IOS                               \
	ios_base::sync_with_stdio(false); \
	cin.tie(0);                       \
	cout.tie(0);                      \
	cin.exceptions(cin.failbit);
#define int long long int //Comment when Unnecessary
#define ll long long
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define pii pair<int, int>
#define vi vector<int>
#define F first
#define S second
#define endl "\n" // Remove on Interactive

const int N = 300009;
const int mod = 1000000007;

int n, x;
map<int, int> mp;
vector<pii> zi;
vector<int> z;
vector<int> see;
vector<int> pre;

int32_t main()
{
	IOS;
	cin >> n;
	rep(i, 0, n)
	{
		cin >> x;
		mp[x]++;
	}
	trav(it, mp)
	{
		z.push_back(it.S);
		zi.push_back({it.S, it.F});
	}
	sort(all(zi));
	rep(i, 0, sz(zi))
	{
		rep(j, 0, zi[i].F)
		{
			see.push_back(zi[i].S);
		}
	}
	sort(all(z));
	pre.push_back(0);
	rep(i, 0, sz(z))
	{
		pre.push_back(pre[i] + z[i]);
	}
	int mx = 0;
	int ansi, ansj;
	rep(i, 1, n + 1)
	{
		int take = 0;
		auto it = lower_bound(all(z), i);
		int itne = it - z.begin();
		int sum = 0;
		if (itne > 0)
			sum += pre[itne];
		int bade = sz(z) - itne;
		int le = sum + bade * i;
		int mxj = le / i;
		mxj = min((n / i), mxj);
		if (mxj < i)
			continue;
		if (mx < (mxj * i))
		{
			ansi = i, ansj = mxj;
			mx = (mxj * i);
		}
	}
	// cout << ansi << " " << ansj << endl;
	// trav(it, see)
	// {
	// 	cout << it << " ";
	// }
	// cout << endl;
	int put[ansi][ansj];
	int row = 0;
	int col = 0;
	int prevr = 0, prevc = 0;
	rep(i, 0, (ansi * ansj))
	{
		put[row % ansi][col % ansj] = see[i];
		row += 1;
		col += 1;
		if ((row % ansi) == prevr && (col % ansj) == prevc)
		{
			col += 1;
			prevr = (row % ansi);
			prevc = (col % ansj);
		}
	}
	cout << mx << endl;
	cout << ansi << " " << ansj << endl;
	rep(i, 0, ansi)
	{
		rep(j, 0, ansj)
		{
			cout << put[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}


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

#define IOS                               \
	ios_base::sync_with_stdio(false); \
	cin.tie(0);                       \
	cout.tie(0);                      \
	cin.exceptions(cin.failbit);
// #define int long long int //Comment when Unnecessary
#define ll long long
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define pii pair<int, int>
#define vi vector<int>
#define F first
#define S second
#define endl "\n" // Remove on Interactive

const int N = 300009;
const int mod = 1000000007;

int n, x;
map<int, int> mp;
vector<pii> zi;
vector<int> z;
vector<int> see;
vector<int> pre;

int32_t main()
{
	IOS;
	cin >> n;
	rep(i, 0, n)
	{
		cin >> x;
		mp[x]++;
	}
	trav(it, mp)
	{
		z.push_back(it.S);
		zi.push_back({it.S, it.F});
	}
	sort(all(zi));
	rep(i, 0, sz(zi))
	{
		rep(j, 0, zi[i].F)
		{
			see.push_back(zi[i].S);
		}
	}
	sort(all(z));
	pre.push_back(0);
	rep(i, 0, sz(z))
	{
		pre.push_back(pre[i] + z[i]);
	}
	int mx = 0;
	int ansi, ansj;
	rep(i, 1, n + 1)
	{
		int take = 0;
		auto it = lower_bound(all(z), i);
		int itne = it - z.begin();
		int sum = 0;
		if (itne > 0)
			sum += pre[itne];
		int bade = sz(z) - itne;
		int le = sum + bade * i;
		int mxj = le / i;
		mxj = min((n / i), mxj);
		if (mxj < i)
			continue;
		if (mx < (mxj * i))
		{
			ansi = i, ansj = mxj;
			mx = (mxj * i);
		}
	}
	// cout << ansi << " " << ansj << endl;
	// trav(it, see)
	// {
	// 	cout << it << " ";
	// }
	// cout << endl;
	int put[ansi][ansj];
	int row = 0;
	int col = 0;
	int prevr = 0, prevc = 0;
	rep(i, (n - (ansi * ansj)), n)
	{
		put[row % ansi][col % ansj] = see[i];
		row += 1;
		col += 1;
		if ((row % ansi) == prevr && (col % ansj) == prevc)
		{
			row += 1;
			prevr = (row % ansi);
			prevc = (col % ansj);
		}
	}
	cout << mx << endl;
	cout << ansi << " " << ansj << endl;
	rep(i, 0, ansi)
	{
		rep(j, 0, ansj)
		{
			cout << put[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 200100;
const int M = 1e9+7;
map<int,int> cnts;

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
    //freopen("data.out", "w", stdout);
#endif
    int n;
    scanf("%d",&n);
    for(int i = 0,x; i < n; i++)
    {
        cin(x);
        cnts[x]++;
    }
    vector<vector<int>> val_by_cnt(n+1); //出现次数
    for(auto val_cnt : cnts)
    {
        val_by_cnt[val_cnt.second].push_back(val_cnt.first);
    }

    int gep[n+1];
    gep[n] = val_by_cnt[n].size();
    for(int i = n-1; i >= -1; i--)
    {
        gep[i] = gep[i+1] + val_by_cnt[i].size();
    }

    int tot = 0,best = 0,best_a,best_b;
    for(int a = 1; a <= n; a++)
    {
        tot += gep[a];      //一个数最多出现a次,所以有a行答案
        int b = tot/a;      //b列
        if(a <= b && best < a*b)        //求a行b列的最大值咯
        {
            best = a*b;
            best_a = a;
            best_b = b;
        }
    }

    cout<<best<<endl<<best_a<<' '<<best_b<<endl;

    int r[n][n];

    int x = 0, y = 0;   //开始填答案
    for(int i = n; i >= 1; i--)
    {
        for(auto val : val_by_cnt[i])
        {
            for(int j = 0; j < min(i,best_a); j++)
            {
                if(r[x][y] != 0)
                {
                    x = (x+1)%best_a;
                }
                if(r[x][y] == 0)
                {
                    r[x][y] = val;
                }
                x = (x+1)%best_a;
                y = (y+1)%best_b;
            }
        }
    }
    for(int i = 0; i < best_a; i++)
    {
        for(int j = 0; j < best_b; j++)
            cout<<r[i][j]<<' ';
        cout<<endl;
    }
    return 0;
}