#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long INF = LLONG_MAX / 2;
const long long N = 2e5 + 1;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  {
    long long n, m;
    cin >> n >> m;
    vector<long long> first(n);
    queue<long long> q;
    map<long long, long long> d;
    for (long long i = 0; i < n; i++) {
      cin >> first[i];
      d[first[i]] = 0;
      q.push(first[i]);
    }
    long long ans = 0;
    vector<long long> second;
    while (!q.empty()) {
      if (second.size() == m) break;
      long long a = q.front();
      q.pop();
      if (d[a]) {
        ans += d[a];
        second.push_back(a);
      }
      if (!d.count(a - 1)) {
        d[a - 1] = d[a] + 1;
        q.push(a - 1);
      }
      if (!d.count(a + 1)) {
        d[a + 1] = d[a] + 1;
        q.push(a + 1);
      }
    }
    cout << ans << '\n';
    for (auto i : second) cout << i << " ";
  }
  return 0;
}
