OI XXV - tom

// https://szkopul.edu.pl/problemset/problem/Hhip15j-8Ro2dOb_4oB98C-G/site/?key=statement

// Tomik poezji

#include <bits/stdc++.h>

using namespace std;

#define int long long

constexpr int sizik = 1000 * 1001;

// #define GARY_DBG

#define ar std::array
#define pr std::pair
#define vec std::vector

typedef vec<vec<int>> _kra;

int op[sizik];
std::vector<int> ans;
std::set<std::pair<int, int>> secix;

void dodaj(int x) {
    auto ptr = secix.lower_bound({x, -1});

    if (ptr != secix.end() && ptr->first == x) {
        ans.push_back(ptr->second);
        secix.erase(ptr);
    } else {
        std::cout << "ERROR dodaj(" << x << ")\n";
    }
}

void solve() {
    int n, s;
    std::cin >> n >> s;

    for (int i = 1; i <= n; i++) {
        int a;
        std::cin >> a;

        a++;
        a %= s;
        if (a != 0) {
            op[a]++;
            // d[a].push(i);
            secix.insert({a, i});
        } else {
            ans.push_back(i);
        }
    }

    std::priority_queue<std::pair<int, int>> q;

    for (int i = 1; i <= s - 1; i++) {
        if (op[i] > 0) {
            q.push({op[i], i});
        }
    }

    int w = 0, ansix = 0;
    while (q.size() > 1) {
        const auto [a, b] = q.top();
        q.pop();

        if (((w + b) % s) == s - 1) {
            const auto [e, f] = q.top();
            q.pop();

            dodaj(f);
            if (e > 1) q.push({e - 1, f});
            q.push({a, b});

            w = (w + f) % s;
        } else {
            dodaj(b);
            if (a > 1) q.push({a - 1, b});
            w = (w + b) % s;
        }
    }

    if (!q.empty()) {
        const auto [a, b] = q.top();
        q.pop();

        for (int i = 0; i < a; i++) {
            if (w == s - 1) {
                ansix++;
                w = 0;
            }
            dodaj(b);
            w = (w + b) % s;
        }
    }

    std::cout << ansix << '\n';
    for (const auto& a : ans) {
        std::cout << a << ' ';
    }
    std::cout << '\n';
}

int32_t main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int t = 1;
    // std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}