#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
map<int, bool> us;
vector<int> a;
vector<int> p;
int n, m;
long long ans = 0;
void foo(int deep) {
  vector<int> b;
  for (int i = 0; i < a.size(); i++) {
    int x1 = a[i] - 1;
    int x2 = a[i] + 1;
    if (p.size() < m && !us[x1]) {
      b.push_back(x1);
      p.push_back(x1);
      us[x1] = 1;
      ans += deep;
    }
    if (p.size() < m && !us[x2]) {
      b.push_back(x2);
      p.push_back(x2);
      us[x2] = 1;
      ans += deep;
    }
  }
  a = b;
  if (p.size() < m) foo(deep + 1);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
    us[x] = 1;
  }
  sort(a.begin(), a.end());
  foo(1);
  cout << ans << endl;
  for (int i = 0; i < p.size(); i++) {
    cout << p[i] << " ";
  }
  return 0;
}
