/*input
4 5
1 2 5 4
1 2 0 4
5 0 0 3
0 0 0 3
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i,j,k)     for(int i = j ; i < k ; ++i)
#define RREP(i,j,k)    for(int i = j ; i >=k ; --i)
#define A    first
#define B    second
#define mp   make_pair
#define pb   emplace_back
#define PII pair<int , int>
#define MEM(i,j)   memset(i , j , sizeof i)
#define ALL(i)     i.begin() , i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0) , cout.sync_with_stdio(0)
#define endl "\n"
///------------------------------------------------------------
#define MAX 200
#define INF 0x3f3f3f3f

struct N{
    int id , x , y;
};
vector<N> ans;
int n , k , in , a[MAX] , aa[MAX] , b[MAX] , bb[MAX] , u[MAX];
PII x[MAX] , wt[MAX];
int can(int now){
    if(abs(wt[now].A - x[now].A) == 1 && wt[now].B == x[now].B){
        ans.pb(N{now , wt[now].A , wt[now].B});
        u[now] = 1;
        return 1;
    }
    else return 0;
}
int put(int idx , PII now){
    REP(i , 1 , k + 1) if(u[i] == 0 && x[i] == now) return 0;
    x[idx] = now;
    ans.pb(N{idx , now.A , now.B});
    return 1;
}
int solve(int now){
    if(x[now].A == 2){
        if(x[now].B == 1 && put(now , mp(3 , 1))) return 1;
        else if(put(now , mp(2 , x[now].B - 1)))  return 1;
    }
    if(x[now].A == 3){
        if(x[now].B == n && put(now , mp(2 , n))) return 1;
        else if(put(now , mp(3 , x[now].B + 1)))  return 1;
    }
    return 0;
}
int32_t main(){
    IOS;
    cin >> n >> k;
    REP(i , 1 , n + 1) cin >> a[i] , wt[a[i]] = mp(1 , i);
    REP(i , 1 , n + 1) {
        cin >> in , aa[i] = in;
        if(in != 0) x[in] = mp(2 , i);
    }
    REP(i , 1 , n + 1){
        cin >> in , bb[i] = in;
        if(in != 0) x[in] = mp(3 , i);
    }
    REP(i , 1 , n + 1) cin >> b[i] , wt[b[i]] = mp(4 , i);

    vector<int> sol;
    REP(i , 1 , n + 1) if(aa[i] != 0) sol.pb(aa[i]);
    RREP(i , n , 1)    if(bb[i] != 0) sol.pb(bb[i]);

    int ok = 0;

    while(ok != k){
        REP(i , 1 , k + 1) if(u[i] == 0 && can(i)) u[i] = 1 , ok ++;
        int move = 0 , pos = 0;
        while(move != k - ok){
            if(u[sol[pos]] == 1) pos ++ , pos = (pos + 1) % sol.size();
            else {
                if(solve(sol[pos])) move ++;
                pos = (pos + 1) % sol.size();
            }
    //      if(pos == 0 && move == 0){
    // for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    //          return cout << -1 << endl , 0;
    //      }
            if(pos == 0 && move == 0) return cout << -1 << endl , 0;
        }
    }
    for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    return 0;
}


/*input
4 5
1 2 0 4
1 2 0 4
5 0 0 3
0 5 0 3
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i,j,k)     for(int i = j ; i < k ; ++i)
#define RREP(i,j,k)    for(int i = j ; i >=k ; --i)
#define A    first
#define B    second
#define mp   make_pair
#define pb   emplace_back
#define PII pair<int , int>
#define MEM(i,j)   memset(i , j , sizeof i)
#define ALL(i)     i.begin() , i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0) , cout.sync_with_stdio(0)
#define endl "\n"
///------------------------------------------------------------
#define MAX 200
#define INF 0x3f3f3f3f

struct N{
    int id , x , y;
};
vector<N> ans;
int n , k , in , a[MAX] , aa[MAX] , b[MAX] , bb[MAX] , u[MAX];
PII x[MAX] , wt[MAX];
int can(int now){
    if(abs(wt[now].A - x[now].A) == 1 && wt[now].B == x[now].B){
        ans.pb(N{now , wt[now].A , wt[now].B});
        u[now] = 1;
        return 1;
    }
    else return 0;
}
int put(int idx , PII now){
    REP(i , 1 , k + 1) if(u[i] == 0 && x[i] == now) return 0;
    x[idx] = now;
    ans.pb(N{idx , now.A , now.B});
    return 1;
}
int solve(int now){
    if(x[now].A == 2){
        if(x[now].B == 1 && put(now , mp(3 , 1))) return 1;
        else if(put(now , mp(2 , x[now].B - 1)))  return 1;
    }
    if(x[now].A == 3){
        if(x[now].B == n && put(now , mp(2 , n))) return 1;
        else if(put(now , mp(3 , x[now].B + 1)))  return 1;
    }
    return 0;
}
int32_t main(){
    IOS;
    cin >> n >> k;
    REP(i , 1 , n + 1) cin >> a[i] , wt[a[i]] = mp(1 , i);
    REP(i , 1 , n + 1) {
        cin >> in , aa[i] = in;
        if(in != 0) x[in] = mp(2 , i);
    }
    REP(i , 1 , n + 1){
        cin >> in , bb[i] = in;
        if(in != 0) x[in] = mp(3 , i);
    }
    REP(i , 1 , n + 1) cin >> b[i] , wt[b[i]] = mp(4 , i);

    vector<int> sol;
    REP(i , 1 , n + 1) if(aa[i] != 0) sol.pb(aa[i]);
    RREP(i , n , 1)    if(bb[i] != 0) sol.pb(bb[i]);

    int ok = 0;

    while(ok != k){
        REP(i , 1 , k + 1) if(u[i] == 0 && can(i)) u[i] = 1 , ok ++;
        int move = 0 , pos = 0;
        while(move != k - ok){
            if(u[sol[pos]] == 1) pos ++ , pos = (pos + 1) % sol.size();
            else {
                if(solve(sol[pos])) move ++;
                pos = (pos + 1) % sol.size();
            }
    //      if(pos == 0 && move == 0){
    // for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    //          return cout << -1 << endl , 0;
    //      }
            if(pos == 0 && move == 0) return cout << -1 << endl , 0;
        }
    }
    cout << ans.size() << endl;
    for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#include <random>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <list>
#include <cassert>

using namespace std;

typedef long long ll;

int a[4][53];

int n;
struct triple {
    int a, b, c;
    triple(int a, int b, int c) : a(a), b(b), c(c) {}
};

vector <triple> ans;

bool find() {
    bool bad = true;
    for (int i = 0; i < n; ++i) {
        if (a[1][i] == a[0][i] && a[1][i]) {
            bad = false;
            ans.emplace_back(a[0][i], 1, i + 1);
            a[0][i] = a[1][i] = 0;
            break;
        }
        if (a[2][i] == a[3][i] && a[2][i]) {
            ans.emplace_back(a[2][i], 4, i + 1);
            bad = false;
            a[2][i] = a[3][i] = 0;
            break;
        }
    }
    if (!bad) return 1;
    else return 0;
}

void next(int x, int y, int &nx, int &ny) {
    if (x == 1) {
        if (y == n - 1) {
            nx = 2;
            ny = n - 1;
        }
        else {
            nx = 1;
            ny = y + 1;
        }
    }
    else {
        if (y == 0) {
            nx = 1;
            ny = 0;
        }
        else {
            nx = 2;
            ny = y - 1;
        }
    }
}

bool ok() {
    bool bad = false;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] != 0) {
                bad = true;
                break;
            }
        }
        if (bad) break;
    }
    return !bad;
}

void shift() {
    int x = -1, y = -1;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 0) {
                x = i;
                y = j;
            }
        }
    }
    assert(x != -1 && y != -1);
    int nx, ny;
    int tx = x, ty = y;
    next(x, y, nx, ny);
    while (nx != tx || ny != ty) {
        a[x][y] = a[nx][ny];
        if (a[nx][ny]) ans.emplace_back(a[nx][ny], x + 1, y + 1);
        x = nx;
        y = ny;
        next(nx, ny, nx, ny);
    }
    a[x][y] = 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
        	cin >> a[i][j];
        }
    }
    if (k == 2 * n) {
        if (!find()) {
            cout << "-1\n";
            return 0;
        }
    }
    while (!ok()) {
        if (!find()) {
            shift();
        }
        /*for (int i = 0; i < 4; ++i) {
        	for (int j = 0; j < n; ++j) {
        		cout << a[i][j] << ' ';
        	}
        	cout << '\n';
        }
        cout << '\n'; */
    }
    cout << ans.size() << '\n';
    for (auto &e : ans) {
        cout << e.a << ' ' << e.b << ' ' << e.c << '\n';
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#include <random>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <list>
#include <cassert>

using namespace std;

typedef long long ll;

int a[4][50];

int n;
struct triple {
    int a, b, c;
    triple(int a, int b, int c) : a(a), b(b), c(c) {}
};

vector <triple> ans;

bool find() {
    bool bad = true;
    for (int i = 0; i < n; ++i) {
        if (a[1][i] == a[0][i] && a[1][i]) {
            bad = false;
            ans.emplace_back(a[0][i], 1, i + 1);
            a[0][i] = a[1][i] = 0;
            break;
        }
        if (a[2][i] == a[3][i] && a[2][i]) {
            ans.emplace_back(a[2][i], 4, i + 1);
            bad = false;
            a[2][i] = a[3][i] = 0;
            break;
        }
    }
    if (!bad) return 1;
    else return 0;
}

void next(int x, int y, int &nx, int &ny) {
    if (x == 1) {
        if (y == n - 1) {
            nx = 2;
            ny = n - 1;
        }
        else {
            nx = 1;
            ny = y + 1;
        }
    }
    else {
        if (y == 0) {
            nx = 1;
            ny = 0;
        }
        else {
            nx = 2;
            ny = y - 1;
        }
    }
}

bool ok() {
    bool bad = false;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] != 0) {
                bad = true;
                break;
            }
        }
        if (bad) break;
    }
    return !bad;
}

void shift() {
    int x = -1, y = -1;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 0) {
                x = i;
                y = j;
            }
        }
    }
    assert(x != -1 && y != -1);
    int nx, ny;
    int tx = x, ty = y;
    next(x, y, nx, ny);
    while (nx != tx || ny != ty) {
        a[x][y] = a[nx][ny];
        ans.emplace_back(a[nx][ny], x + 1, y + 1);
        x = nx;
        y = ny;
        next(nx, ny, nx, ny);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
        	cin >> a[i][j];
        }
    }
    if (k == 2 * n) {
        if (!find()) {
            cout << "-1\n";
            return 0;
        }
    }
    while (!ok()) {
        if (!find()) {
            shift();
        }
    }
    cout << ans.size() << '\n';
    for (auto &e : ans) {
        cout << e.a << ' ' << e.b << ' ' << e.c << '\n';
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#include <random>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <list>
#include <cassert>

using namespace std;

typedef long long ll;

int a[4][50];

int n;
struct triple {
    int a, b, c;
    triple(int a, int b, int c) : a(a), b(b), c(c) {}
};

vector <triple> ans;

bool find() {
    bool bad = true;
    for (int i = 0; i < n; ++i) {
        if (a[1][i] == a[0][i] && a[1][i]) {
            bad = false;
            ans.emplace_back(a[0][i], 1, i + 1);
            a[0][i] = a[1][i] = 0;
            break;
        }
        if (a[2][i] == a[3][i] && a[2][i]) {
            ans.emplace_back(a[2][i], 4, i + 1);
            bad = false;
            a[2][i] = a[3][i] = 0;
            break;
        }
    }
    if (!bad) return 1;
    else return 0;
}

void next(int x, int y, int &nx, int &ny) {
    if (x == 1) {
        if (y == n - 1) {
            nx = 2;
            ny = n - 1;
        }
        else {
            nx = 1;
            ny = y + 1;
        }
    }
    else {
        if (y == 0) {
            nx = 1;
            ny = 0;
        }
        else {
            nx = 2;
            ny = y - 1;
        }
    }
}

bool ok() {
    bool bad = false;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] != 0) {
                bad = true;
                break;
            }
        }
        if (bad) break;
    }
    return !bad;
}

void shift() {
    int x = -1, y = -1;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 0) {
                x = i;
                y = j;
            }
        }
    }
    assert(x != -1 && y != -1);
    int nx, ny;
    int tx = x, ty = y;
    next(x, y, nx, ny);
    while (nx != tx || ny != ty) {
        a[x][y] = a[nx][ny];
        if (a[nx][ny]) ans.emplace_back(a[nx][ny], x + 1, y + 1);
        x = nx;
        y = ny;
        next(nx, ny, nx, ny);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
        	cin >> a[i][j];
        }
    }
    if (k == 2 * n) {
        if (!find()) {
            cout << "-1\n";
            return 0;
        }
    }
    while (!ok()) {
        if (!find()) {
            shift();
        }
    }
    cout << ans.size() << '\n';
    for (auto &e : ans) {
        cout << e.a << ' ' << e.b << ' ' << e.c << '\n';
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#include <random>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <list>
#include <cassert>

using namespace std;

typedef long long ll;

int a[4][53];

int n;
struct triple {
    int a, b, c;
    triple(int a, int b, int c) : a(a), b(b), c(c) {}
};

vector <triple> ans;

bool find() {
    bool bad = true;
    for (int i = 0; i < n; ++i) {
        if (a[1][i] == a[0][i] && a[1][i]) {
            bad = false;
            ans.emplace_back(a[0][i], 1, i + 1);
            a[0][i] = a[1][i] = 0;
            break;
        }
        if (a[2][i] == a[3][i] && a[2][i]) {
            ans.emplace_back(a[2][i], 4, i + 1);
            bad = false;
            a[2][i] = a[3][i] = 0;
            break;
        }
    }
    if (!bad) return 1;
    else return 0;
}

void next(int x, int y, int &nx, int &ny) {
    if (x == 1) {
        if (y == n - 1) {
            nx = 2;
            ny = n - 1;
        }
        else {
            nx = 1;
            ny = y + 1;
        }
    }
    else {
        if (y == 0) {
            nx = 1;
            ny = 0;
        }
        else {
            nx = 2;
            ny = y - 1;
        }
    }
}

bool ok() {
    bool bad = false;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] != 0) {
                bad = true;
                break;
            }
        }
        if (bad) break;
    }
    return !bad;
}

void shift() {
    int x = -1, y = -1;
    for (int i = 1; i <= 2; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 0) {
                x = i;
                y = j;
            }
        }
    }
    assert(x != -1 && y != -1);
    int nx, ny;
    int tx = x, ty = y;
    next(x, y, nx, ny);
    while (nx != tx || ny != ty) {
        a[x][y] = a[nx][ny];
        if (a[nx][ny]) ans.emplace_back(a[nx][ny], x + 1, y + 1);
        x = nx;
        y = ny;
        next(nx, ny, nx, ny);
    }
    a[x][y] = 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
        	cin >> a[i][j];
        }
    }
    if (k == 2 * n) {
        if (!find()) {
            cout << "-1\n";
            return 0;
        }
    }
    while (!ok()) {
        if (!find()) {
            shift();
        }
        /*for (int i = 0; i < 4; ++i) {
        	for (int j = 0; j < n; ++j) {
        		cout << a[i][j] << ' ';
        	}
        	cout << '\n';
        }
        cout << '\n'; */
    }
    cout << ans.size() << '\n';
    for (auto &e : ans) {
        cout << e.a << ' ' << e.b << ' ' << e.c << '\n';
    }
}


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

#if DEBUG
#include "prettyprint.hpp"
#define PRINTLN(x) \
    cerr << x << "\n"
#define PRINTSP(x) \
    cerr << x << " "
#else
#define PRINTLN(x)
#define PRINTSP(x)
#endif

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> void amax(T & a, const T & b) { a = max(a,b); }
template<class T> void amin(T & a, const T & b) { a = min(a,b); }

template<class T> T getinword () {
    T temp;
    cin >> temp;
    return temp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> l (4, vector<int> (n));
    for (int i=0; i < n; i++) cin >> l[0][i];
    for (int i=0; i < n; i++) cin >> l[1][i];
    for (int i=0; i < n; i++) cin >> l[2][i];
    for (int i=0; i < n; i++) cin >> l[3][i];

    vector<tuple<int, int, int>> mov;
    while (k > 0) {
        for (int i=0; i < n; i++) {
            if (l[1][i] != 0 && l[1][i] == l[0][i]) {
                k--;
                mov.push_back({l[1][i], 1, i+1});
                l[1][i] = 0;
            }
            if (l[2][i] != 0 && l[2][i] == l[3][i]) {
                k--;
                mov.push_back({l[2][i], 4, i+1});
                l[2][i] = 0;
            }
        }
        if (k == 2*n) {
            cout << "-1\n";
            return 0;
        }
        vector<tuple<int, int, int>> mov2;
        bool fz = false;
        int tmp = l[2][0];
        for (int i=0; i < n-1; i++) {
            if (l[2][i+1] != 0) {
                if (fz)
                    mov.push_back({l[2][i+1], 3, i+1});
                else
                    mov2.push_back({l[2][i+1], 3, i+1});
            }
            else {
                fz = true;
            }
            l[2][i] = l[2][i+1];
        }
        if (l[1][n-1] != 0) {
            if (fz)
                mov.push_back({l[1][n-1], 3, n});
            else
                mov2.push_back({l[1][n-1], 3, n});
        }
        else {
            fz = true;
        }
        l[2][n-1] = l[1][n-1];
        for (int i=n-1; i > 0; i--) {
            if (l[1][i-1] != 0) {
                if (fz)
                    mov.push_back({l[1][i-1], 2, i+1});
                else
                    mov2.push_back({l[1][i-1], 2, i+1});
            }
            else {
                fz = true;
            }
            l[1][i] = l[1][i-1];
        }
        if (tmp != 0) {
            if (fz)
                mov.push_back({tmp, 2, 1});
            else
                mov2.push_back({tmp, 2, 1});
        }
        l[1][0] = tmp;
        mov.insert(mov.end(), all(mov2));
        PRINTLN("l[0]:  " << l[0]);
        PRINTLN("l[1]:  " << l[1]);
        PRINTLN("l[2]:  " << l[2]);
        PRINTLN("l[3]:  " << l[3]);
        PRINTLN("-------------");
    }

    cout << mov.size() << "\n";
    for (auto [a, b, c] : mov) {
        cout << a << " " << b << " " << c << "\n";
    }
}


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

#if DEBUG
#include "prettyprint.hpp"
#define PRINTLN(x) \
    cerr << x << "\n"
#define PRINTSP(x) \
    cerr << x << " "
#else
#define PRINTLN(x)
#define PRINTSP(x)
#endif

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> void amax(T & a, const T & b) { a = max(a,b); }
template<class T> void amin(T & a, const T & b) { a = min(a,b); }

template<class T> T getinword () {
    T temp;
    cin >> temp;
    return temp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> l (4, vector<int> (n));
    for (int i=0; i < n; i++) cin >> l[0][i];
    for (int i=0; i < n; i++) cin >> l[1][i];
    for (int i=0; i < n; i++) cin >> l[2][i];
    for (int i=0; i < n; i++) cin >> l[3][i];

    vector<tuple<int, int, int>> mov;
    while (k > 0) {
        for (int i=0; i < n; i++) {
            if (l[1][i] != 0 && l[1][i] == l[0][i]) {
                k--;
                mov.push_back({l[1][i], 1, i+1});
                l[1][i] = 0;
            }
            if (l[2][i] != 0 && l[2][i] == l[3][i]) {
                k--;
                mov.push_back({l[2][i], 4, i+1});
                l[2][i] = 0;
            }
        }
        if (k == 2*n) {
            cout << "-1\n";
            return 0;
        }
        int tmp = l[2][0];
        for (int i=0; i < n-1; i++) {
            if (l[2][i+1] != 0) {
                mov.push_back({l[2][i+1], 3, i+1});
            }
            l[2][i] = l[2][i+1];
        }
        if (l[1][n-1] != 0) {
            mov.push_back({l[1][n-1], 3, n});
        }
        l[2][n-1] = l[1][n-1];
        for (int i=n-1; i > 0; i--) {
            if (l[1][i-1] != 0) {
                mov.push_back({l[1][i-1], 2, i+1});
            }
            l[1][i] = l[1][i-1];
        }
        if (tmp != 0) {
            mov.push_back({tmp, 2, 1});
        }
        l[1][0] = tmp;
        PRINTLN("l[0]:  " << l[0]);
        PRINTLN("l[1]:  " << l[1]);
        PRINTLN("l[2]:  " << l[2]);
        PRINTLN("l[3]:  " << l[3]);
        PRINTLN("-------------");
    }

    cout << mov.size() << "\n";
    for (auto [a, b, c] : mov) {
        cout << a << " " << b << " " << c << "\n";
    }
}


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

typedef long long ll;
typedef pair <int , int> pii;

const int N = 1e2+10 , mod = 1e9+7;

vector < pair <int , pii> > ans;
pii curr[N];
int a[N][N] , b[N][N];

void Move(int id , int dir){
   if(!id)
      return;
   int x = curr[id].first , y = curr[id].second;
   a[x][y] = 0;
   if(dir == 0)
      x--;
   else if(dir == 1)
      y++;
   else if(dir == 2)
      x++;
   else
      y--;
   curr[id] = {x , y};
   a[x][y] = id;
   ans.push_back({id , curr[id]});
}

int32_t main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   int n , k;
   cin >> n >> k;
   for(int i = 0 ; i < 4 ; i++)
      for(int j = 0 ; j < n ; j++){
         if(i != 0 && i != 3){
            cin >> a[i][j];
            curr[a[i][j]] = {i , j};
         }
         else{
            cin >> b[i][j];
         }
      }
   if(n == 1){
      if(k == 2 && b[0][0] != a[1][0])
         return cout << -1 , 0;
      if(k == 2){
         Move(a[1][0] , 0);
         Move(a[2][0] , 2);
      }
      else{
         if(b[0][0] == a[2][0])
            Move(a[2][0] , 0);
         if(b[0][0] == a[1][0])
            Move(a[1][0] , 0);
         if(b[3][0] == a[1][0])
            Move(a[1][0] , 2);
         if(b[3][0] == a[2][0])
            Move(a[2][0] , 2);
      }
   }
   else{
   for(int i = 0 ; i < n ; i++){
      if(a[1][i])
         Move(a[1][i] , 0);
      if(a[2][i])
         Move(a[2][i] , 2);
   }
   for(int i = 0 ; i < n ; i++){
      if(!b[0][i])
         continue;
      int id = b[0][i];
      int x = curr[id].first , y = curr[id].second;
      int xx = x , yy = y;
      if(curr[id] == make_pair(0 , i))
         continue;
      if(x == 0){
         Move(id , 2);
         Move(id , 2);
      }
      else{
         Move(id , 0);
      }
      x = 2;
      while(y > i){
         Move(id , 3);
         y--;
      }
      while(y < i){
         Move(id , 1);
         y++;
      }
      int id2 = a[0][i];
      Move(id2 , 2);
      if(i < n - 1)
         Move(id2 , 1);
      else
         Move(id2 , 3);
      Move(id , 0);
      Move(id , 0);
      x = curr[id2].first;
      y = curr[id2].second;
      while(y < yy){
         Move(id2 , 1);
         y++;
      }
      while(y > yy){
         Move(id2 , 3);
         y--;
      }
      Move(id2 , 2);
      Move(id2 , 2);
   }
   for(int i = 0 ; i < n ; i++)
      if(a[3][i])
         Move(a[3][i] , 0);
   for(int i = 0 ; i < n ; i++){
      if(!a[1][i]){
         bool fnd = false;
         for(int j = i ; j >= 0 ; j--){
            if(a[2][j]){
               fnd = true;
               for(int k = 0 ; k < i - j ; k++)
                  Move(a[2][j + k] , 1);
               Move(a[2][i] , 0);
               break;
            }
         }
         for(int j = i + 1 ; j < n && !fnd ; j++){
            if(a[2][j]){
               fnd = true;
               for(int k = 0 ; k < j - i ; k++)
                  Move(a[2][j - k] , 3);
               Move(a[2][i] , 0);
               break;
            }
         }
      }
   }
   for(int i = 0 ; i < n ; i++){
      if(!b[3][i])
         continue;
      int id = b[3][i];
      int x = curr[id].first , y = curr[id].second;
      Move(id , 2);
      if(y > i){
         for(int j = y ; j > i ; j--)
            Move(id , 3);
      }
      else{
         for(int j = y ; j < i ; j++)
            Move(id , 1);
      }
      Move(id , 2);
   }
   }
   cout << ans.size() << "\n";
   for(auto x : ans)
      cout << x.first << " " << x.second.first + 1 << " " << x.second.second + 1 << "\n";
   return 0;
}


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

typedef long long ll;
typedef pair <int , int> pii;

const int N = 1e2+10 , mod = 1e9+7;

vector < pair <int , pii> > ans;
pii curr[N];
int a[N][N] , b[N][N];

bool moved;

void Move(int id , int dir){
   if(!id)
      return;
   moved = true;
   int x = curr[id].first , y = curr[id].second;
   a[x][y] = 0;
   if(dir == 0)
      x--;
   else if(dir == 1)
      y++;
   else if(dir == 2)
      x++;
   else
      y--;
   curr[id] = {x , y};
   a[x][y] = id;
   ans.push_back({id , curr[id]});
   if(x == 1 && id == b[0][y]){
      Move(id , 0);
   }
   if(x == 2 && id == b[3][y]){
      Move(id , 2);
   }
}

int32_t main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   int n , k;
   cin >> n >> k;
   for(int i = 0 ; i < 4 ; i++)
      for(int j = 0 ; j < n ; j++){
         if(i != 0 && i != 3){
            cin >> a[i][j];
            curr[a[i][j]] = {i , j};
         }
         else{
            cin >> b[i][j];
         }
      }
   if(k == 2 * n){
      bool fnd = false;
      for(int i = 0 ; i < n ; i++){
         if(b[0][i] == a[1][i])
            Move(a[1][i] , 0) , fnd = true;
         if(b[3][i] == a[2][i])
            Move(a[2][i] , 2) , fnd = true;
      }
      if(!fnd)
         return cout << -1 , 0;
   }
   moved = true;
   while(moved){
      moved = false;
      if(!a[2][n - 1])
         Move(a[1][n - 1] , 2);
      for(int i = n - 2 ; i >= 0 ; i--)
         if(!a[1][i + 1])
            Move(a[1][i] , 1);
      if(!a[1][0])
         Move(a[2][0] , 0);
      for(int i = 1 ; i < n ; i++)
         if(!a[2][i - 1])
            Move(a[2][i] , 3);
   }
   cout << ans.size() << "\n";
   for(auto x : ans)
      cout << x.first << " " << x.second.first + 1 << " " << x.second.second + 1 << "\n";
   return 0;
}


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

typedef long long ll;
typedef pair <int , int> pii;

const int N = 1e2+10 , mod = 1e9+7;

vector < pair <int , pii> > ans;
pii curr[N];
int a[N][N] , b[N][N];

void Move(int id , int dir){
   if(!id)
      return;
   int x = curr[id].first , y = curr[id].second;
   a[x][y] = 0;
   if(dir == 0)
      x--;
   else if(dir == 1)
      y++;
   else if(dir == 2)
      x++;
   else
      y--;
   curr[id] = {x , y};
   a[x][y] = id;
   ans.push_back({id , curr[id]});
}

int32_t main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   int n , k;
   cin >> n >> k;
   for(int i = 0 ; i < 4 ; i++)
      for(int j = 0 ; j < n ; j++){
         if(i != 0 && i != 3){
            cin >> a[i][j];
            curr[a[i][j]] = {i , j};
         }
         else{
            cin >> b[i][j];
         }
      }
   if(n == 1){
      if(k == 2 && b[0][0] != a[1][0])
         return cout << -1 , 0;
      if(k == 2){
         Move(a[1][0] , 0);
         Move(a[2][0] , 2);
      }
      else{
         if(b[0][0] == a[2][0])
            Move(a[2][0] , 0);
         if(b[0][0] == a[1][0])
            Move(a[1][0] , 0);
         if(b[3][0] == a[1][0])
            Move(a[1][0] , 2);
         if(b[3][0] == a[2][0])
            Move(a[2][0] , 2);
      }
   }
   else{
   for(int i = 0 ; i < n ; i++){
      if(!b[0][i])
         continue;
      int id = b[0][i];
      int x = curr[id].first , y = curr[id].second;
      if(x == 1){
         if(y <= i){
            for(int j = y + 1 ; j <= i ; j++){
               if(a[2][j])
                  Move(a[2][j] , 2);
               if(a[1][j])
                  Move(a[1][j] , 2);
               Move(id , 1);
            }
            Move(id , 0);
            for(int j = y + 1 ; j <= i ; j++){
               if(a[2][j])
                  Move(a[2][j] , 0);
               if(a[3][j])
                  Move(a[3][j] , 0);
            }
         }
         else{
            Move(id , 0);
            for(int j = y ; j > i ; j--)
               Move(id , 3);
         }
      }
      else{
         Move(id , 2);
         for(int i = y ; i < n - 1 ; i++)
            Move(id , 1);
         Move(a[2][n - 2] , 2);
         Move(a[1][n - 2] , 2);
         Move(a[1][n - 1] , 3);
         Move(a[2][n - 1] , 0);
         Move(a[3][n - 1] , 0);
         Move(a[3][n - 2] , 1);
         Move(a[2][n - 2] , 2);
         Move(a[1][n - 2] , 2);
         Move(a[1][n - 1] , 3);
         Move(a[2][n - 1] , 0);
         Move(a[1][n - 1] , 0);
         for(int j = n - 1 ; j > i ; j--)
            Move(a[0][j] , 3);
         Move(a[2][n - 2] , 0);
         Move(a[3][n - 2] , 0);
         Move(a[2][n - 1] , 0);
         Move(a[3][n - 1] , 0);
      }
   }
   for(int i = 0 ; i < n ; i++){
      if(!a[1][i]){
         bool fnd = false;
         for(int j = i ; j >= 0 ; j--){
            if(a[2][j]){
               fnd = true;
               for(int k = 0 ; k < i - j ; k++)
                  Move(a[2][j + k] , 1);
               Move(a[2][i] , 0);
               break;
            }
         }
         for(int j = i + 1 ; j < n && !fnd ; j++){
            if(a[2][j]){
               fnd = true;
               for(int k = 0 ; k < j - i ; k++)
                  Move(a[2][j - k] , 3);
               Move(a[2][i] , 0);
               break;
            }
         }
      }
   }
   for(int i = 0 ; i < n ; i++){
      if(!b[3][i])
         continue;
      int id = b[3][i];
      int x = curr[id].first , y = curr[id].second;
      Move(id , 2);
      if(y > i){
         for(int j = y ; j > i ; j--)
            Move(id , 3);
      }
      else{
         for(int j = y ; j < i ; j++)
            Move(id , 1);
      }
      Move(id , 2);
   }
   }
   cout << ans.size() << "\n";
   for(auto x : ans)
      cout << x.first << " " << x.second.first + 1 << " " << x.second.second + 1 << "\n";
   return 0;
}


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

struct Move {
    Move (int v, int x, int y) : v(v), x(x), y(y) {}
    int v, x, y;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

    vector<Move> moves;

    // try to park at least one car
    for (int i = 0; i < n; i++) {
        if (v[1][i] && v[1][i] == v[0][i]) {
            moves.emplace_back(v[1][i], 0, i);
            k--;
            v[1][i] = 0;
        }
        if (v[2][i] && v[2][i] == v[3][i]) {
            moves.emplace_back(v[2][i], 3, i);
            k--;
            v[2][i] = 0;
        }
    }

    if (k == 2 * n) {
        cout << -1 << '\n';
        return 0;
    }

    // find empty
    vector<pair<int, int>> vp;
    for (int i = 0; i < n; i++) {
        vp.push_back({1, i});
    }
    for (int i = n-1; i >= 0; i--) {
        vp.push_back({2, i});
    }
    int idx = 0;
    for (int i = 0; i < 2*n; i++) {
        auto [x, y] = vp[i];
        if (v[x][y] == 0) {
            idx = i;
            break;
        }
    }

    while (k) {
        for (int i = 0; i < 2*n - 1; i++) {
            // swap idx + i, idx + i + 1
            auto [x, y] = vp[(idx + i) % (2*n)];
            auto [x2, y2] = vp[(idx + i + 1) % (2*n)];
            if (v[x2][y2]) {
                moves.emplace_back(v[x2][y2], x, y);
                swap(v[x2][y2], v[x][y]);
            }
        }
        idx = (idx + 2*n - 1) % (2*n);

        // try to park cars
        for (int i = 0; i < n; i++) {
            if (v[1][i] && v[1][i] == v[0][i]) {
                moves.emplace_back(v[1][i], 0, i);
                k--;
                v[1][i] = 0;
            }
            if (v[2][i] && v[2][i] == v[3][i]) {
                moves.emplace_back(v[2][i], 3, i);
                k--;
                v[2][i] = 0;
            }
        }
    }

    cout << moves.size() << '\n';
    for (auto move : moves) {
        cout << move.v << " " << move.x + 1 << " " << move.y + 1 << endl;
    }
}


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

struct Move {
    Move (int v, int x, int y) : v(v), x(x), y(y) {}
    int v, x, y;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

    vector<Move> moves;

    // try to park at least one car
    for (int i = 0; i < n; i++) {
        if (v[1][i] && v[1][i] == v[0][i]) {
            moves.emplace_back(v[1][i], 0, i);
            k--;
            v[1][i] = 0;
        }
        if (v[2][i] && v[2][i] == v[3][i]) {
            moves.emplace_back(v[2][i], 3, i);
            k--;
            v[2][i] = 0;
        }
    }

    if (k == 2 * n) {
        cout << -1 << '\n';
        return 0;
    }

    // find empty
    vector<pair<int, int>> vp;
    for (int i = 0; i < n; i++) {
        vp.push_back({1, i});
    }
    for (int i = n-1; i >= 0; i--) {
        vp.push_back({2, i});
    }
    int idx = 0;
    for (int i = 0; i < 2*n; i++) {
        auto [x, y] = vp[i];
        if (v[x][y] == 0) {
            idx = i;
            break;
        }
    }

    while (k) {
        for (int i = 0; i < 2*n - 1; i++) {
            // swap idx + i, idx + i + 1
            auto [x, y] = vp[(idx + i) % (2*n)];
            auto [x2, y2] = vp[(idx + i + 1) % (2*n)];
            if (v[x2][y2]) {
                moves.emplace_back(v[x2][y2], x, y);
                swap(v[x2][y2], v[x][y]);
            }
        }

        // try to park cars
        for (int i = 0; i < n; i++) {
            if (v[1][i] && v[1][i] == v[0][i]) {
                moves.emplace_back(v[1][i], 0, i);
                k--;
                v[1][i] = 0;
            }
            if (v[2][i] && v[2][i] == v[3][i]) {
                moves.emplace_back(v[2][i], 3, i);
                k--;
                v[2][i] = 0;
            }
        }
    }

    cout << moves.size() << '\n';
    for (auto move : moves) {
        cout << move.v << " " << move.x + 1 << " " << move.y + 1 << endl;
    }
}


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

struct Move {
    Move (int v, int x, int y) : v(v), x(x), y(y) {}
    int v, x, y;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

    vector<Move> moves;

    // try to park at least one car
    for (int i = 0; i < n; i++) {
        if (v[1][i] && v[1][i] == v[0][i]) {
            moves.emplace_back(v[1][i], 0, i);
            k--;
            v[1][i] = 0;
        }
        if (v[2][i] && v[2][i] == v[3][i]) {
            moves.emplace_back(v[2][i], 3, i);
            k--;
            v[2][i] = 0;
        }
    }

    if (k == 2 * n) {
        cout << -1 << '\n';
        return 0;
    }

    // find empty
    vector<pair<int, int>> vp;
    for (int i = 0; i < n; i++) {
        vp.push_back({1, i});
    }
    for (int i = n-1; i >= 0; i--) {
        vp.push_back({2, i});
    }
    int idx = 0;
    for (int i = 0; i < 2*n; i++) {
        auto [x, y] = vp[i];
        if (v[x][y] == 0) {
            idx = i;
            break;
        }
    }

    while (k) {
        for (int i = 0; i < 2*n; i++) {
            // swap idx + i, idx + i + 1
            auto [x, y] = vp[(idx + i) % (2*n)];
            auto [x2, y2] = vp[(idx + i + 1) % (2*n)];
            if (v[x2][y2]) {
                moves.emplace_back(v[x2][y2], x, y);
                swap(v[x2][y2], v[x][y]);
            }
        }

        // try to park cars
        for (int i = 0; i < n; i++) {
            if (v[1][i] && v[1][i] == v[0][i]) {
                moves.emplace_back(v[1][i], 0, i);
                k--;
                v[1][i] = 0;
            }
            if (v[2][i] && v[2][i] == v[3][i]) {
                moves.emplace_back(v[2][i], 3, i);
                k--;
                v[2][i] = 0;
            }
        }
    }

    cout << moves.size() << '\n';
    for (auto move : moves) {
        cout << move.v << " " << move.x + 1 << " " << move.y + 1 << endl;
    }
}


#include <iostream>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define pb push_back

const ll MAX = 60;

ll n, k;
pll pos[MAX], place[MAX];
ll gr[MAX][5];
vector<pair<ll, pll>> ans;
pll start = {1, 2};

ll getDist(ll p) {
    return abs(place[p].first - pos[p].first) + abs(place[p].second - pos[p].second);
}

bool check() {
    for(ll y = 2; y <= 3; y++) {
        for(ll i = 1; i <= n; i++) {
            if(gr[i][y] != 0 && getDist(gr[i][y]) == 1) {
                start = {i, y};
                return true;
            }
        }
    }
    return false;
}

pll next(ll x, ll y) {
    if(y == 2) {
        if(x == 1) {
            y = 3;
        } else {
            x--;
        }
    } else {
        if(x == n) {
            y = 2;
        } else {
            x++;
        }
    }
    return {x, y};
}

pll prev(ll x, ll y) {
    if(y == 2) {
        if(x == n) {
            y = 3;
        } else {
            x++;
        }
    } else {
        if(x == 1) {
            y = 2;
        } else {
            x--;
        }
    }
    return {x, y};
}

void move(pll from, pll to) {
    ans.pb({ gr[from.first][from.second], {to.second, to.first} });

    pos[gr[from.first][from.second]] = to;
    gr[to.first][to.second] = gr[from.first][from.second];
    gr[from.first][from.second] = 0;
}

void solve() {
    scanf("%lld %lld", &n, &k);
    for(ll i = 1; i <= n; i++) {
        ll cur;
        scanf("%lld", &cur);
        if(cur != 0) {
            place[cur] = {i, 1};
        }
        gr[i][1] = cur;
    }
    for(ll i = 2; i < 4; i++) {
        for(ll j = 1; j <= n; j++) {
            ll cur;
            scanf("%lld", &cur);
            if(cur != 0) {
                pos[cur] = {j, i};
            }
            gr[j][i] = cur;
        }
    }
    for(ll i = 1; i <= n; i++) {
        ll cur;
        scanf("%lld", &cur);
        if(cur != 0) {
            place[cur] = {i, 4};
        }
        gr[i][4] = cur;
    }

    if(k == 2 * n && !check()) {
        printf("-1\n");
        return;
    } else {
        for(ll i = 0; i < 2 * n; i++) {
            pll cur = next(start.first, start.second);
            ll cx = cur.first;
            ll cy = cur.second;
            if(gr[cx][cy] != 0) {
                pll kek = next(cx, cy);
                if(gr[kek.first][kek.second] == 0) {
                    start = kek;
                    break;
                }
            }
        }
    }
    ll ost = k;
    while(ost) {
        if(gr[start.first][start.second] != 0) {
            if(getDist(gr[start.first][start.second]) == 1) {
                move(start, place[gr[start.first][start.second]]);
                start = prev(start.first, start.second);
                ost--;
                continue;
            }
        }
        //printf("Pos %lld %lld\n", start.first, start.second);
        if(gr[start.first][start.second] == 0) {
            start = prev(start.first, start.second);
            continue;
        }
        pll to = next(start.first, start.second);
        move(start, to);
        start = prev(start.first, start.second);

    }
    printf("%lld\n", (ll)ans.size());
    for(ll i = 0; i < ans.size(); i++) {
        printf("%lld %lld %lld\n", ans[i].first, ans[i].second.first, ans[i].second.second);
    }
}

int main() {
    solve();

    return 0;
}


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int dp[20][5];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof dp);
        memset(ct,0,sizeof ct);
        m+=2;
        // int l=m;
        int stop=0;
        int p=0;
        int flag=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=-1;
            ct[i].r=-1;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
            }
            if (ct[i].l!=-1 || ct[i].r!=-1)
                flag=1;
            if (ct[i].l==-1 && ct[i].r==-1 && flag==0)
                stop=i;
            if (ct[i].l==-1)
                ct[i].l=m-1;
            if (ct[i].r==-1)
                ct[i].r=0;
        }
        dp[n][0]=0;
        dp[n][1]=0;
        for (int i=n;i>0;i--)
        {
            int l=ct[i].l;
            int r=ct[i].r;
            if (i!=n)
            {
                dp[i-1][0]=min(dp[i][0]+2*r+1,dp[i][1]+m);
                dp[i-1][1]=min(dp[i][0]+m,dp[i][1]+2*(m-1-l)+1);
            }
            else
            {
                dp[i-1][0]=dp[i][0]+2*r+1;
                dp[i-1][1]=m;
            }
        }
        int l=ct[stop+1].l;
        int r=ct[stop+1].r;
        // cout<<stop<<" "<<l<<" "<<r<<endl;
        // cout<<dp[stop+1][0]<<" "<<dp[stop+1][1]<<endl;
        int ans;
        if (stop+1!=n)
            ans=min(dp[stop+1][0]+r,dp[stop+1][1]+(m-1-l));
        else
            ans=r;
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0000
0100

2 4
000010
001000

3 4
001000
000000
000010

4 3
01110
01110
01110
01110

*/


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        m+=2;
        // int l=m;
        int stop=-1;
        int p=0;
        int flag=0;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=0;
            ct[i].r=0;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
            }
            if (ct[i].l!=0 || ct[i].r!=0)
                flag=1;
            if (ct[i].l==0 && ct[i].r==0 && flag==0)
            {
                flag=1;
                stop=i;
            }
        }
        int l,r;
        int mid=-1;
        int ans=0;
        for (int i=n-1;i>stop+1;i--)
        {
           l=ct[i].l;
           r=ct[i].r;
           mid=m/2;
           // cout<<l<<" "<<r<<endl;

           if (l==r && l==0)
           {
                ans++;
                continue;
            }
           if (m%2==0)
           {
                mid=m/2;
                if (p==0)
                {
                    if (r>=mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else
                        ans+=r*2;
                }
                else
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else
                        ans+=(m-1-l)*2;
                }
           }
           else
           {
                if (p==0)
                {
                    if (r>mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else if (r<mid)
                        ans+=r*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else if (p==1)
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else if (l>mid)
                        ans+=(m-1-l)*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else
                {
                    if (l<=mid && r>=mid)
                    {
                        ans+=m-1;
                    }
                    else if (l>mid)
                    {
                        ans+=(m-1-l)*2;
                        p=1;
                    }
                    else if (r<mid)
                    {
                        ans+=r*2;
                        p=0;
                    }
                }
           }
           ans++;
           // cout<<"bug "<<ans<<endl;
        }
        l=ct[stop+1].l;
        r=ct[stop+1].r;
        mid=m/2;
        if (p==0)
            ans+=r;
        else if (p==1)
            ans+=(m-1-l);
        else
            ans+=min(r,(m-1-l));
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0010
0100

2 2
0000
0000

4 3
00000
00100
00000
00100

3 4
001000
000010
000010

*/


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        m+=2;
        // int l=m;
        int stop=-1;
        int p=0;
        int flag=0;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=0;
            ct[i].r=0;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
            }
            if (ct[i].l==0 && ct[i].r==0 && flag==0)
            {
                flag=1;
                stop=i;
            }
        }
        int l,r;
        int mid=-1;
        int ans=0;
        for (int i=n-1;i>stop+1;i--)
        {
           l=ct[i].l;
           r=ct[i].r;
           mid=m/2;
           // cout<<l<<" "<<r<<endl;

           if (l==r && l==0)
            continue;
           if (m%2==0)
           {
                mid=m/2;
                if (p==0)
                {
                    if (r>=mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else
                        ans+=r*2;
                }
                else
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else
                        ans+=(m-1-l)*2;
                }
           }
           else
           {
                if (p==0)
                {
                    if (r>mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else if (r<mid)
                        ans+=r*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else if (p==1)
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else if (l>mid)
                        ans+=(m-1-l)*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else
                {
                    if (l<=mid && r>=mid)
                    {
                        ans+=m-1;
                    }
                    else if (l>mid)
                    {
                        ans+=(m-1-l)*2;
                        p=1;
                    }
                    else if (r<mid)
                    {
                        ans+=r*2;
                        p=0;
                    }
                }
           }
           ans++;
           // cout<<"bug "<<ans<<endl;
        }
        l=ct[stop+1].l;
        r=ct[stop+1].r;
        mid=m/2;
        if (p==0)
            ans+=r;
        else if (p==1)
            ans+=(m-1-l);
        else
            ans+=min(r,(m-1-l));
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0010
0100

2 2
0000
0000

3 4
000000
000110
000010

3 4
001000
000010
000010

4 3
01110
01110
01110
01110

*/


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        m+=2;
        // int l=m;
        int stop=-1;
        int p=0;
        int flag=0;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=0;
            ct[i].r=0;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
            }
            if (ct[i].l==0 && ct[i].r==0 && flag==0)
            {
                flag=1;
                stop=i;
            }
        }
        int l,r;
        int mid=-1;
        int ans=0;
        for (int i=n-1;i>stop+1;i--)
        {
           l=ct[i].l;
           r=ct[i].r;
           mid=m/2;
           // cout<<l<<" "<<r<<endl;

           if (l==r && l==0)
           {
                ans++;
                continue;
            }
           if (m%2==0)
           {
                mid=m/2;
                if (p==0)
                {
                    if (r>=mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else
                        ans+=r*2;
                }
                else
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else
                        ans+=(m-1-l)*2;
                }
           }
           else
           {
                if (p==0)
                {
                    if (r>mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else if (r<mid)
                        ans+=r*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else if (p==1)
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else if (l>mid)
                        ans+=(m-1-l)*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else
                {
                    if (l<=mid && r>=mid)
                    {
                        ans+=m-1;
                    }
                    else if (l>mid)
                    {
                        ans+=(m-1-l)*2;
                        p=1;
                    }
                    else if (r<mid)
                    {
                        ans+=r*2;
                        p=0;
                    }
                }
           }
           ans++;
           // cout<<"bug "<<ans<<endl;
        }
        l=ct[stop+1].l;
        r=ct[stop+1].r;
        mid=m/2;
        if (p==0)
            ans+=r;
        else if (p==1)
            ans+=(m-1-l);
        else
            ans+=min(r,(m-1-l));
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0010
0100

2 2
0000
0000

4 3
00000
00100
00000
00100

*/


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        m+=2;
        // int l=m;
        int p=0;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=0;
            ct[i].r=0;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
            }
        }
        int l,r;
        int mid=-1;
        int ans=0;
        for (int i=n-1;i>=0;i--)
        {
           l=ct[i].l;
           r=ct[i].r;
           mid=m/2;
           // cout<<l<<" "<<r<<endl;

           if (l==r && l==0)
            continue;
           if (m%2==0)
           {
                mid=m/2;
                if (p==0)
                {
                    if (r>=mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else
                        ans+=r*2;
                }
                else
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else
                        ans+=(m-1-l)*2;
                }
           }
           else
           {
                if (p==0)
                {
                    if (r>mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else if (r<mid)
                        ans+=r*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else if (p==1)
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else if (l>mid)
                        ans+=(m-1-l)*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else
                {
                    if (l<=mid && r>=mid)
                    {
                        ans+=m-1;
                    }
                    else if (l>mid)
                    {
                        ans+=(m-1-l)*2;
                        p=1;
                    }
                    else if (r<mid)
                    {
                        ans+=r*2;
                        p=0;
                    }
                }
           }
           if (i)
                ans++;
           // cout<<"bug "<<ans<<endl;
        }
        if (p==0)
        {
            for (int i=0;i<m;i++)
            {
                if (s[0][i]=='0')
                    ans--;
                else
                    break;
            }
        }
        else if (p==1)
        {
            for (int i=m-1;i>=0;i--)
            {
                if (s[0][i]=='0')
                    ans--;
                else
                    break;
            }
        }
        else
        {
            int a1=0,a2=0;
            for (int i=0;i<m;i++)
            {
                if (s[0][i]=='0')
                    a1++;
                else
                    break;
            }
            for (int i=m-1;i>=0;i--)
            {
                if (s[0][i]=='0')
                    a2++;
                else
                    break;
            }
            ans-=max(a1,a2);
        }
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0010
0100

3 4
001000
000010
000010

4 3
01110
01110
01110
01110

*/


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        m+=2;
        // int l=m;
        int stop=-1;
        int p=0;
        int flag=0;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=0;
            ct[i].r=0;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
                flag=1;
            }
            if (ct[i].l==0 && ct[i].r==0 && flag==0)
            {
                flag=1;
                stop=i;
            }
        }
        int l,r;
        int mid=-1;
        int ans=0;
        for (int i=n-1;i>stop+1;i--)
        {
           l=ct[i].l;
           r=ct[i].r;
           mid=m/2;
           // cout<<l<<" "<<r<<endl;

           if (l==r && l==0)
           {
                ans++;
                continue;
            }
           if (m%2==0)
           {
                mid=m/2;
                if (p==0)
                {
                    if (r>=mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else
                        ans+=r*2;
                }
                else
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else
                        ans+=(m-1-l)*2;
                }
           }
           else
           {
                if (p==0)
                {
                    if (r>mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else if (r<mid)
                        ans+=r*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else if (p==1)
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else if (l>mid)
                        ans+=(m-1-l)*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else
                {
                    if (l<=mid && r>=mid)
                    {
                        ans+=m-1;
                    }
                    else if (l>mid)
                    {
                        ans+=(m-1-l)*2;
                        p=1;
                    }
                    else if (r<mid)
                    {
                        ans+=r*2;
                        p=0;
                    }
                }
           }
           ans++;
           // cout<<"bug "<<ans<<endl;
        }
        l=ct[stop+1].l;
        r=ct[stop+1].r;
        mid=m/2;
        if (p==0)
            ans+=r;
        else if (p==1)
            ans+=(m-1-l);
        else
            ans+=min(r,(m-1-l));
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0010
0100

2 2
0000
0000

4 3
00100
00100
00010
00000

*/


#include <cstdio>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 100007;
const int M = 15005;
const int offset = 7500;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

int T,n,m;
int k;
char s[20][N];
struct nd
{
    int l,r;
}ct[20];
int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        m+=2;
        // int l=m;
        int stop=-1;
        int p=0;
        int flag=0;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            int f=0;
            ct[i].l=0;
            ct[i].r=0;
            for (int j=0;j<m;j++)
            {
                if (s[i][j]=='1' && f==0)
                {
                    f=1;
                    ct[i].l=j;
                }
                if (s[i][j]=='1')
                    ct[i].r=j;
            }
            if (ct[i].l!=0 || ct[i].r!=0)
                flag=1;
            if (ct[i].l==0 && ct[i].r==0 && flag==0)
            {
                // flag=1;
                stop=i;
            }
        }
        int l,r;
        int mid=-1;
        int ans=0;
        for (int i=n-1;i>stop+1;i--)
        {
           l=ct[i].l;
           r=ct[i].r;
           mid=m/2;
           // cout<<l<<" "<<r<<endl;

           if (l==r && l==0)
           {
                ans++;
                continue;
            }
           if (m%2==0)
           {
                mid=m/2;
                if (p==0)
                {
                    if (r>=mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else
                        ans+=r*2;
                }
                else
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else
                        ans+=(m-1-l)*2;
                }
           }
           else
           {
                if (p==0)
                {
                    if (r>mid)
                    {
                        p=1;
                        ans+=m-1;
                    }
                    else if (r<mid)
                        ans+=r*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else if (p==1)
                {
                    if (l<mid)
                    {
                        p=0;
                        ans+=m-1;
                    }
                    else if (l>mid)
                        ans+=(m-1-l)*2;
                    else
                    {
                        ans+=m-1;
                        p=3;
                    }
                }
                else
                {
                    if (l<=mid && r>=mid)
                    {
                        ans+=m-1;
                    }
                    else if (l>mid)
                    {
                        ans+=(m-1-l)*2;
                        p=1;
                    }
                    else if (r<mid)
                    {
                        ans+=r*2;
                        p=0;
                    }
                }
           }
           ans++;
           // cout<<"bug "<<ans<<endl;
        }
        l=ct[stop+1].l;
        r=ct[stop+1].r;
        mid=m/2;
        if (p==0)
            ans+=r;
        else if (p==1)
            ans+=(m-1-l);
        else
            ans+=min(r,(m-1-l));
        printf("%d\n", ans);
    }

    return 0;
}

/*

2 2
0010
0100

2 2
0000
0000

4 3
00010
00100
00000
00100

3 4
001000
000010
000010

*/


#include <bits/stdc++.h>

using namespace std;

int main() {
    // freopen("in.in","r",stdin);
    // freopen("out.out","w",stdout);
    int n,m ;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<m; j++) {

            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
        if(f==0) {
            level[i].first = -1;
            level[i].second = -1;
        }
    }

    int cnt = 0;
    bool dir  = 1 ;
    // 1 -> left
    // 0 -> right
    int i = n-1;
    while(i>=0 && level[i].second == -1)
        i--,cnt++;
    if(i>=0)
        cnt += level[i].second;
    else
        cnt=0;
    int j = i-1,c=1;
    for(; j>=0; j--) {
        cnt+=level[i].second-level[i].first;
        if(level[j].first  != -1) {
            cnt+=c+min(level[i].first+level[j].first,(m-1-level[i].second)+(m-1-level[j].second)) ;
            c=1;
            i--;
        } else
            c++;
    }
    cout << cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    // freopen("in.in","r",stdin);
    // freopen("out.out","w",stdout);
    int n,m ;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<m; j++) {

            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
        if(f==0) {
            level[i].first = -1;
            level[i].second = -1;
        }
    }

    int cnt = 0;
    bool dir  = 1 ;
    // 1 -> left
    // 0 -> right
    int i = n-1;
    while(i>=0 && level[i].second == -1)
        i--,cnt++;
    if(i>=0)
        cnt += level[i].second;
    else
        cnt=0;
    int j = i-1,c=1;
    for(; j>=0; j--) {
           // cout <<cnt <<" "<< i << " " << j<< endl;
        cnt+=level[j].second-level[j].first;
       // cout <<cnt <<endl;
        if(level[j].first  != -1) {
            cnt+=c+min(level[i].first+level[j].first,(m-1-level[i].second)+(m-1-level[j].second)) ;
            c=1;
            i--;
        } else
            c++;
       // cout <<cnt <<endl;
    }
    cout << cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    // freopen("in.in","r",stdin);
    // freopen("out.out","w",stdout);
    int n,m ;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<=m; j++) {

            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
        if(f==0) {
            level[i].first = -1;
            level[i].second = -1;
        }
    }

    int cnt = 0;
    bool dir  = 1 ;
    // 1 -> left
    // 0 -> right
    int i = n-1;
    while(i>=0 && level[i].second == -1)
        i--,cnt++;
    if(i>=0)
        cnt += level[i].second;
    else
        cnt=0;
    int j = i-1,c=1;
    for(; j>=0; j--) {
        cnt+=level[i].second-level[i].first;
        if(level[j].first  != -1) {
            cnt+=c+min(level[i].first+level[j].first,(m-1-level[i].second)+(m-1-level[j].second)) ;
            c=1;
            i--;
        } else
            c++;
    }
    cout << cnt<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    // freopen("in.in","r",stdin);
    // freopen("out.out","w",stdout);
    int n,m ;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<=m; j++) {

            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
    }

    int cnt = level[n-1].second;
    for(int i=n-1; i>0; i--)
        cnt+= 1+min(level[i].first+level[i-1].first,(m-1-level[i].second)+(m-1-level[i-1].second)) +level[i].second-level[i].first;
    cout << cnt<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    // freopen("in.in","r",stdin);
    // freopen("out.out","w",stdout);
    int n,m ;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<m; j++) {

            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
        if(f==0) {
            level[i].first = -1;
            level[i].second = -1;
        }
    }

    int cnt = 0;
    bool dir  = 1 ;
    // 1 -> left
    // 0 -> right
    int i = n-1;
    while(i>=0 && level[i].second == -1)
        i--,cnt++;
    if(i>=0)
        cnt += level[i].second;
    else
        cnt=0;
    int j = i-1,c=1;
    for(; j>0; j--) {
        cnt+=level[i].second-level[i].first;
        if(level[j].first  != -1) {
            cnt+=c+min(level[i].first+level[j].first,(m-1-level[i].second)+(m-1-level[j].second)) ;
            c=1;
            i--;
        } else
            c++;
    }
    cout << cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int m;
int diff(pair<int,int> pr) {
    return pr.second - pr.first;
}
int to(pair<int,int> pr) {
    return m - pr.second;
}
int test_me(vector < pair <int,int> > act,int i,bool  dir) {
    if(i == 0)
        return 0;
    int c = 0;
    if(dir == 1) {
        c += 1+ to(act[i])+to(act[i-1])+diff(act[i-1])+ test_me(act,i-1,0);
        c = min(c,1+act[i].first+act[i-1].first+diff(act[i])+ diff(act[i-1]) +test_me(act,i-1,1));
    } else {
        c += 1+ act[i].first+act[i-1].first+ diff(act[i-1]) +test_me(act,i-1,1);
        c = min(c,1+ to(act[i])+to(act[i-1])+diff(act[i])+diff(act[i-1]) +test_me(act,i-1,0));
    }
    return c;
}
int main() {
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
    int n;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<m; j++) {
            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
        if(f==0) {
            level[i].first = -1;
            level[i].second = -1;
        }
    }

    int cnt = 0,c=0;
    bool f=0;
    vector< pair <int,int> > actual;
    for(int i = 0; i<n; i++) {
        if(level[i].first==-1&&f==1)
            c++;
        if(level[i].first!=-1) {
            f=1;
            actual.push_back(level[i]);
        }
    }

    m--;
    if(actual.size() > 0) {
        cnt+=c;
        cnt+=actual[(int)actual.size()-1].second;
        cnt+=test_me(actual,(int)actual.size()-1,1);

    }
    cout << cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int m;
int diff(pair<int,int> pr) {
    return pr.second - pr.first;
}
int to(pair<int,int> pr) {
    return m - pr.second;
}
int test_me(vector < pair <int,int> > act,int i,bool  dir) {
    if(i == 0)
        return 0;
    int c = 0;
    if(dir == 1) {
        c += 1+ to(act[i])+to(act[i-1])+diff(act[i-1])+ test_me(act,i-1,0);
        c = min(c,1+act[i].first+act[i-1].first+diff(act[i])+ diff(act[i-1]) +test_me(act,i-1,1));
    } else {
        c += 1+ act[i].first+act[i-1].first+ diff(act[i-1]) +test_me(act,i-1,1);
        c = min(c,1+ to(act[i])+to(act[i-1])+diff(act[i])+diff(act[i-1]) +test_me(act,i-1,0));
    }
    return c;
}
int main() {
   // freopen("in.in","r",stdin);
    //freopen("out.out","w",stdout);
    int n;
    cin >>  n >>  m;
    m+=2;
    int arr[n][m];
    vector < pair <int,int> > level(n);
    for(int i=0; i<n; i++) {
        bool f=0;
        string str;
        cin >> str;
        for(int j=0; j<m; j++) {
            arr[i][j] = (str[j] - '0');
            if(arr[i][j]==1&&f==0)
                level[i].first=j,f=1;
            if(arr[i][j]==1)
                level[i].second=j;
        }
        if(f==0) {
            level[i].first = -1;
            level[i].second = -1;
        }
    }

    int cnt = 0,c=0;
    bool f=0;
    vector< pair <int,int> > actual;
    for(int i = 0; i<n; i++) {
        if(level[i].first==-1&&f==1)
            c++;
        if(level[i].first!=-1) {
            f=1;
            actual.push_back(level[i]);
        }
    }

    m--;
    if(actual.size() > 0) {
        cnt+=c;
        cnt+=actual[(int)actual.size()-1].second;
        cnt+=test_me(actual,(int)actual.size()-1,1);

    }
    cout << cnt<<endl;
    return 0;
}


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

 vector<string> Blocks;
 long long dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }
}

 int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     for( i = 0 ; i < N ; i-- ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );
  dp[N - 1][0] = r << 1;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r << 1;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + 1LL * (r << 1 ) + 1LL;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + 1LL * m + 2LL;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + 1LL * ( (m + 1 - l) << 1) + 1LL;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + 1LL * m + 2ll;
    }

  long long Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


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

 vector<string> Blocks;
 int dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }
}

 int main(){
  // ios::sync_with_stdio(false);
  // cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     if( N == 1 ){
      Find_Dir( 0 );
      cout << min( 2 * r , m + 1 ) << endl;
      return 0;
    }

     for( i = 0 ; i < N ; i++ ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );

  dp[N - 1][0] = r << 1;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r << 1;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + (r << 1 ) + 1;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + m + 2;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + ((m + 1 - l) << 1) + 1;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + m + 2;
    }

  int Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


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

 vector<string> Blocks;
 long long dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }
}

 int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     if( N == 1 ){
      Find_Dir( 0 );
      cout << min( 2 * r , m + 1 ) << endl;
      return 0;
    }

     for( i = 0 ; i < N ; i++ ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );

  dp[N - 1][0] = r << 1;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r << 1;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + 1LL * (r << 1 ) + 1LL;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + 1LL * m + 2LL;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + 1LL * ( (m + 1 - l) << 1) + 1LL;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + 1LL * m + 2ll;
    }

  long long Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


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

 vector<string> Blocks;
 int dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }
}

 int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     if( N == 1 ){
      Find_Dir( 0 );
      cout << min( 2 * r , m + 1 ) << endl;
      return 0;
    }

     for( i = 0 ; i < N ; i++ ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );

  dp[N - 1][0] = r << 1;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r << 1;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + (r << 1 ) + 1;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + m + 2;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + ((m + 1 - l) << 1) + 1;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + m + 2;
    }

  int Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


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

 vector<string> Blocks;
 int dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }

      if( l == 50 )
       l = 0;
}

 int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     if( N == 1 ){
      Find_Dir( 0 );
      cout << min( 2 * r , m + 1 ) << endl;
      return 0;
    }

     for( i = 0 ; i < N ; i++ ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );

  dp[N - 1][0] = r * 2;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r * 2;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + r * 2 + 1;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + m + 2;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + (m + 1 - l) * 2 + 1;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + m + 2;
    }

  int Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


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

 vector<string> Blocks;
 long long dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }
}

 int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     for( i = 0 ; i < N ; i++ ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );

     if( N == 1 ){
      cout << min( 2 * r , m + 1 ) << endl;
      return 0;
    }

  dp[N - 1][0] = r << 1;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r << 1;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + 1LL * (r << 1 ) + 1LL;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + 1LL * m + 2LL;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + 1LL * ( (m + 1 - l) << 1) + 1LL;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + 1LL * m + 2ll;
    }

  long long Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


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

 vector<string> Blocks;
 int dp[16][4];
 int l , r , N , m;

 void Find_Dir( int row ){
  r = 0;
  l = 50;

      for( int i = 0 ; i < m + 2 ; i++ ){
          if( Blocks[row][i] == '1' ){
           l = min( i , l );
           r = max( r , i );
         }
     }
}

 int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int top , i , j;
  bool Check;
  string Str;
  cin >> N >> m;

     for( i = 1 ; i <= N ; i++ ){
      cin >> Str;
      Blocks.emplace_back( Str );
    }

     if( N == 1 ){
      Find_Dir( 0 );
      cout << min( 2 * r , m + 1 ) << endl;
      return 0;
    }

     for( i = 0 ; i < N ; i++ ){
         for( j = 0 ; j < m + 2 ; j++ )
             if( Blocks[i][j] == '1' )
              Check = 1;

         if( Check )
          break;
    }

  top = i;
  Find_Dir( N - 1 );

  dp[N - 1][0] = r * 2;
  dp[N - 1][1] = m + 1;
  dp[N - 1][2] = m + 1;
  dp[N - 1][3] = r * 2;

     for( i = N - 2 ; i > top ; i-- ){
      Find_Dir( i );

      dp[i][0] = min( dp[i + 1][0] , dp[i + 1][3] ) + r * 2 + 1;
      dp[i][1] = min( dp[i + 1][0] , dp[i + 1][3] ) + m + 2;
      dp[i][2] = min( dp[i + 1][2] , dp[i + 1][1] ) + (m + 1 - l) * 2 + 1;
      dp[i][3] = min( dp[i + 1][2] , dp[i + 1][1] ) + m + 2;
    }

  int Ans;
  Find_Dir( top );
  Ans = min( dp[top + 1][0] , dp[top + 1][3] ) + r + 1;
  Ans = min( Ans , dp[top + 1][1] + m + 2 - l );
  Ans = min( Ans , dp[top + 1][2] + m + 2 - l );
  cout << Ans << endl;
  return 0;
}


    import java.io.*;
    import java.util.Arrays;
    import java.util.StringTokenizer;

    public class NubianMarket {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
            StringTokenizer st = new StringTokenizer(br.readLine());
            long[] souvenirs = new long[Integer.parseInt(st.nextToken())];
            int budget = Integer.parseInt(st.nextToken());
            st = new StringTokenizer(br.readLine());
            for(int i = 0; i < souvenirs.length; i++){
                souvenirs[i] = Long.parseLong(st.nextToken());
            }
            long left = 0;
            long right = souvenirs.length;
            long mid = 0;
            long tempAmount = 0;
            long[] tempSouvenirs = new long[souvenirs.length];
            long sum = 0;
            long numSouvenirs = 0;
            while(left <= right){
                mid = (left + right)/2;
                for(int i = 0; i < souvenirs.length; i++){
                    tempSouvenirs[i] = souvenirs[i] + (mid * (i + 1));
                }
                Arrays.sort(tempSouvenirs);
                tempAmount = budget;
    //            System.out.println(mid + " " + Arrays.toString(tempSouvenirs));
    //            System.out.println(left + " " + mid + " " + right);
                for(int i = 0; i < mid; i++){
                    tempAmount -= tempSouvenirs[i];
                }
                if(tempAmount < 0){
                    right = mid - 1;
                } else{
                    left = mid + 1;
                    numSouvenirs = mid;
                    sum = budget - tempAmount;
                }
            }
            pw.println(numSouvenirs + " " + sum);
            pw.close();
        }
    }


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

public class SagheerAndNubianMarket {
    InputStream is;
    PrintWriter pw;
    String INPUT = "";
    long L_INF = (1L << 60L);

    void solve() {
        int n = ni(), s = ni(), m;
        int a[] = na(n);
        int l = 0, r = n+1;
        while (l < r) {
            m = (l + r) >> 1;
            long val = calc(a, m, s);
            if (val <= s)
                l = m;
            else
                r = m;
        }
        pw.println(l + " " + calc(a, l, s));
    }

    private long calc(int[] a, int m, int s) {
        int n = a.length, idx = 1;
        long c[] = new long[n];
        for (int i = 0; i < n; i++) {
            c[i] = a[i] + (i + 1L) * m;
        }
        Arrays.sort(c, 1, n);
        long sum = 0;
        for (int i = 0; i < m; i++) {
            sum += c[i];
        }
        return sum;
    }

    void run() throws Exception {
//		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        is = System.in;
        pw = new PrintWriter(System.out);

        long s = System.currentTimeMillis();
//        int t = ni();
//        while (t-- > 0)
        solve();
        pw.flush();
        tr(System.currentTimeMillis() - s + "ms");
    }

    public static void main(String[] args) throws Exception {
        new SagheerAndNubianMarket().run();
    }

    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (!oj) System.out.println(Arrays.deepToString(o));
    }
}


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

public class SagheerAndNubianMarket {
    InputStream is;
    PrintWriter pw;
    String INPUT = "";
    long L_INF = (1L << 60L);

    void solve() {
        int n = ni(), s = ni(), m;
        int a[] = na(n);
        int l = 0, r = n+1;
        while (l < r-1) {
            m = (l + r) >> 1;
            long val = calc(a, m, s);
            if (val <= s)
                l = m;
            else
                r = m;
        }
        pw.println(l + " " + calc(a, l, s));
    }

    private long calc(int[] a, int m, int s) {
        int n = a.length, idx = 1;
        long c[] = new long[n];
        for (int i = 0; i < n; i++) {
            c[i] = a[i] + (i + 1L) * m;
        }
        Arrays.sort(c);
        long sum = 0;
        for (int i = 0; i < m; i++) {
            sum += c[i];
        }
        return sum;
    }

    void run() throws Exception {
//		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        is = System.in;
        pw = new PrintWriter(System.out);

        long s = System.currentTimeMillis();
//        int t = ni();
//        while (t-- > 0)
        solve();
        pw.flush();
        tr(System.currentTimeMillis() - s + "ms");
    }

    public static void main(String[] args) throws Exception {
        new SagheerAndNubianMarket().run();
    }

    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (!oj) System.out.println(Arrays.deepToString(o));
    }
}